2017-02-10 3 views
1

2つの異なるデータ型uint8とint32の2つのnumpy配列をファイルにダンプしようとしています。異なるデータ型をnumpyを使用してファイルに保存する際のエラー

img.tofile(PATH + "add_info_to_img.dat") 

# append array_with_info to the beginning of the file 
f_handle = open(PATH + "add_info_to_img.dat", 'a') 
np.savetxt(f_handle, array_with_info) 
f_handle.close() 

データ情報:

img.shape 
Out[4]: (921600,) 
array_with_info.shape 
Out[5]: (5,) 
array_with_info.dtype 
Out[6]: dtype('int32') 
img.dtype 
Out[7]: dtype('uint8') 

任意の提案私は、ファイルを書き込むために、次のコードを使用しています

File "C:\ENV\p34\lib\site-packages\numpy\lib\npyio.py", line 1162, in savetxt 
    % (str(X.dtype), format)) 
TypeError: Mismatch between array dtype ('int32') and format specifier ('%.18e') 

:私は次のエラーを取得していますか?

+0

これはおそらくあなたのデータに関連しています。私は同じタイプのダミーデータでこれを再現することはできません。おそらくデータを知る必要があるでしょう。 – languitar

答えて

0

ファイルはバイナリモードで開いていて、フォーマットを指定する必要があります。

シンプルexemple:

a=arange(3) 
b=arange(3.) 

with open('try.txt','wb') as f: 
    savetxt(f,a,'%d') 
    savetxt(f,b) 

"""  
0 
1 
2 
0.000000000000000000e+00 
1.000000000000000000e+00 
2.000000000000000000e+00 
""" 

しかし、後方が困難になることをお読みください。 おそらく最良の方法がここにあります。np.savez('try2',a,b)

関連する問題