我有一个 float 的 txt 文件,我想在 python 脚本中将其打开为二维 float 列表。如何将 txt 存储为二维 float 列表?我正在使用以下代码:
arrays = []
for line in open('normRowsRaw.txt'):
# use a list comprehension to build your array on the fly
new_array = np.array((array.float(i) for i in line.split(' ')))
arrays.append(new_array)
我的文件就像上面的系列:
0.0098506, 0.068954, 0.007388, 0.0049253, 0.0049253, 0.054178, 0.014776, 0.014776,
0.014776, 0.0024627, 0.007388, 0.0098506, 0.007388, 0.022164, 0.029552, 0.017239,
0.0024627, 0.019701, 0.0098506, 0.0024627, 0, 0.068954, 0.0098506, 0.11082, 0.0024627...
最佳答案
你可以试试这个
arrays = []
with open('normRowsRaw.txt') as f:
f = f.read().split(',')
for n in f:
arrays.append(float(n.strip()))
编辑
这会附加到普通列表而不是 2d
关于python - 将 txt 文件导入二维 float 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995347/