Python 使用numpy将不同数据类型保存到文件时出错

Python 使用numpy将不同数据类型保存到文件时出错,python,numpy,file-io,Python,Numpy,File Io,我试图将两个numpy数组转储到一个文件中,这两个数组具有两种不同的数据类型uint8和int32。我遇到以下错误: 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')

我试图将两个numpy数组转储到一个文件中,这两个数组具有两种不同的数据类型uint8和int32。我遇到以下错误:

 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')
我正在使用以下代码编写该文件:

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')

有什么建议吗

文件必须以二进制模式打开,并且必须指定格式

一个简单的例子:

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)
您的文件必须以二进制模式打开,并且必须指定格式

一个简单的例子:

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)

这可能与您的数据有关。我不能用同一类型的伪数据复制这个。所以我们可能需要知道数据,这可能与你的数据有关。我不能用同一类型的伪数据复制这个。所以我们可能需要知道数据。