【发布时间】:2017-09-10 21:21:56
【问题描述】:
我已经编写了这段代码来创建一个数组=>
import numpy as np
a = np.array([[[1,3],[1,4]],[[1,4],[6,9]]])
np.savetxt('test.txt',a,fmt='%d')
并得到这个错误 =>
Traceback(最近一次调用最后一次):文件 “/usr/local/lib/python3.4/dist-packages/numpy-1.11.2-py3.4-linux-x86_64.egg/numpy/lib/npyio.py”, 第 1158 行,在 savetxt 中 fh.write(asbytes(format % tuple(row) + newline)) TypeError: %d format: a number is required, not numpy.ndarray
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):文件“”,第 1 行,in 文件 “/usr/local/lib/python3.4/dist-packages/numpy-1.11.2-py3.4-linux-x86_64.egg/numpy/lib/npyio.py”, 第 1162 行,在 savetxt 中 % (str(X.dtype), format)) TypeError: 数组 dtype ('int64') 和格式说明符 ('%d %d') 不匹配
如何使用 numpy 将数组保存为文件中的整数?
【问题讨论】:
-
您希望输出文件采用什么样的结构?你能发布你想要的吗?
-
@roganjosh 我想将该矩阵保存在该 txt 文件中
-
您有一个 3d 数组,请尝试保存
a.reshape(-1, 2)。np.savetxt('test.txt',a.reshape(-1,2),fmt='%d') -
好的,谢谢你帮助我。完美的@Psidom