我正在尝试将两个不同数据类型uint8和int32的numpy数组转储到一个文件中。我收到了以下错误:
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')
有什么建议吗?
答案 0 :(得分:0)
您的文件必须以二进制模式打开,并且您必须指定格式。
一个简单的例子:
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)