【发布时间】:2018-10-28 23:35:18
【问题描述】:
假设我使用 numpy 得到了这个 ndarray,我想写入一个文本文件。 :
[[1 2 3 4]
[5 6 7 8]
[9 10 11 12]]
这是我的代码:
width, height = matrix.shape
with open('file.txt', 'w') as foo:
for x in range(0, width):
for y in range(0, height):
foo.write(str(matrix[x, y]))
foo.close()
问题是我在一行中得到了 ndarray 的所有行,但是我希望它像这样写入文件:
1 2 3 4
5 6 7 8
9 10 11 12
【问题讨论】:
标签: python-3.x text-files numpy-ndarray