【发布时间】:2021-03-16 09:39:38
【问题描述】:
我有一个二进制文件,其中散布着数据段。我知道每个数据段的位置(字节偏移),以及这些数据段的大小,以及数据点的类型(float、float32 - 意味着每个数据点都由 4 个字节编码)。我想将这些数据段读入一个类似结构的数组(例如,numpy 数组或 pandas 数据帧),但这样做有困难。我试过使用 numpy 的 memmap,但它在最后一个数据段上短路了,而 numpy 从文件中得到的结果很奇怪。
代码示例:
begin=datadf["$BEGINDATA"][0] #datadf is pandas.df that has where data begins and its size
buf.seek(begin) #buf is the file that is opened in rb mode
size=datadf["$DATASIZE"][0]+1 #same as the above
data=buf.read(size) #this should get me that data segment, but in binary
有没有办法从这个二进制数据可靠地转换为 float32。 为了进一步说明,我将前 10 个数据点的打印输出包括在内。
buf.seek(begin)
print(buf.read(40)) #10 points of float32 (4bytes) means 40
>>>b'\xa5\x10[@\x00\x00\x88@a\xf3\xf7A\x00\x00\x88@&\x93\x9bA\x00\x00\x88@\x00\x00\x00@\xfc\xcd\x08?\x1c\xe2\xbe?\x03\xf9\xa4?'
如果是任意值,虽然每个浮点数有 4 个字节(32 位宽),但每个浮点数的最大值为 10 000
【问题讨论】:
标签: python python-3.x numpy binaryfiles