以二进制格式保存后,Python数组大小加倍

时间:2021-06-15 21:41:51

I have data stored in an array of size (4320,2160), reshaped from a list of length 4320*2160. When I save the file in binary format using numpy's tofile method, and then open the file I noticed that the array is double in length. How do I get the original values of the array? I'm assuming it has something to do with endianness, but i'm unfamiliar with dealing with it.

我将数据存储在一个大小为(4320,2160)的数组中,从长度为4320 * 2160的列表中重新整形。当我使用numpy的tofile方法以二进制格式保存文件,然后打开文件时,我注意到数组的长度是双倍的。如何获取数组的原始值?我假设它与字节序有关,但我不熟悉处理它。

cdom=np.reshape(cdom, (4320,2160), order='F') # array of float values
len(cdom) # 4320*2160
cdom.tofile(filename)

arr = np.fromfile(filename, dtype=np.float32)
len(arr) # double the size of cdom: 2*4320*2160

1 个解决方案

#1


It looks like cdom has type np.float64, and you are reading the binary file as np.float32, so the length is doubled (and the values are effectively garbage).

它看起来像cdom有类型np.float64,你正在读取二进制文件为np.float32,所以长度加倍(并且值实际上是垃圾)。

#1


It looks like cdom has type np.float64, and you are reading the binary file as np.float32, so the length is doubled (and the values are effectively garbage).

它看起来像cdom有类型np.float64,你正在读取二进制文件为np.float32,所以长度加倍(并且值实际上是垃圾)。