【发布时间】:2017-07-13 07:07:02
【问题描述】:
我有一个<class 'numpy.ndarray'> 对象,我希望将它保存在一个txt 文件中。该对象的尺寸(形状)(130, 118, 118) 和大小1810120。
当我尝试将np.savetxt(f, object, delimiter=' ', fmt='1.10f') 与f = open('test.txt', 'wb') 一起使用时,我收到错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\G****\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1139, in savetxt
raise error
ValueError: fmt has wrong number of % formats: 1.10f
我尝试了 1.f 的各种组合,但都没有奏效。有人建议吗?
更新:
在遵循 cmets 的建议后,添加fmt='%1.10f'
我收到了这个:
Traceback (most recent call last):
File "C:\Users\G****\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1158, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: only length-1 arrays can be converted to Python scalars
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\G****\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1162, in savetxt
% (str(X.dtype), format))
TypeError: Mismatch between array dtype ('int8') and format specifier ('%1.10f...... the '%1.10f goes on for quite a while)
【问题讨论】:
-
试试:
fmt='%1.10f'