2017-02-23 515 views
0

我有一个<class 'numpy.ndarray'>对象,我希望将它保存在txt文件中。该物体具有尺寸(形状)(130, 118, 118)和尺寸1810120将numpy ndarray保存为一个txt文件

当我尝试使用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各种组合,但没有工作过。建议任何人?

UPDATE: 以下从意见建议波纹管,并添加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) 
+0

尝试:'fmt ='%1.10f'' –

回答

1

尝试增加%,例如使用fmt='%1.10f'。请参阅here

更新:

import numpy as np 

obj = np.random.randint(10, size=(3, 4, 5), dtype=np.int8) # example array 

with open('test.txt', 'wb') as f: 
    np.savetxt(f, np.column_stack(obj), fmt='%1.10f') 

请注意,在最后一行的np.column_stack(obj)和阅读this找出为什么它被用在这里。但是,如果您的numpy数组包含整数,您可能需要使用fmt='%s'。此外,np.row_stack(obj)可能是一个有用的替代方案,具体取决于文件的外观。

+0

谢谢!你能再次检查我编辑的答案吗? – Jespar

+0

你的numpy数组是8位整数的元素吗? –

+0

是的,的确如此。这是否意味着我无法拯救他们? – Jespar