numpy:数组dtype('int32')和格式说明符('%d')之间的savetxt不匹配

2024-04-25 22:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用savetxt保存数组。以下是我的工作:

import numpy
import sys
a = numpy.asarray(((1,2,3),(4,5,6)))
numpy.savetxt(sys.stdout, a, fmt='%d')

我得到的是:

Traceback (most recent call last):
  File "C:\Users\George\Documents\My\contests-penza-su-2016\env\lib\site-packages\numpy\lib\npyio.py", line 1158, in savetxt
    fh.write(asbytes(format % tuple(row) + newline))
TypeError: write() argument must be str, not bytes

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\George\Documents\My\contests-penza-su-2016\env\lib\site-packages\numpy\lib\npyio.py", line 1162, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('int32') and format specifier ('%d %d %d')

当我试图输出扁平数组和将其输出到文件时,也会出现类似的错误。


Tags: inimportnumpyformatmostlibsysline