Python 之 将字符串数组转换为浮点型数组

问题描述

现有一 txt 文件,文件共有 M 行,每行有 N 个元素,元素由含小数点后 K 位的 float 组成。现要求读取该文件,输出为 float 型的 list or array 。

代码实现如下

fid = open(file_path,'r')
lines = fid.readlines()
fid.close()
act_feat = []
for j in range(len(lines)):
	if (j%1 == 0):
		line = [float(x) for x in lines[j].split()]
		act_feat.append(line)


你可能感兴趣的:(Python 之 将字符串数组转换为浮点型数组)