繁体   English   中英

我如何从二进制文件读取到python中的int16数组

[英]How do I read from binary file to an array of int16 in python

我正在尝试解析一个二进制文件。 该文件包含一些数据包,每个数据包都以一个时间戳记开始,然后定义一个数组(行和列分别为int32)和数组本身。 我开始尝试解析单个数据包,但读取数组时遇到问题:

tsSize = 8
rowSize = 4
columnSize=4
thresholdVectorSize=4
targetsCandidatesVectorSize=4
centerOfMassVectorSize=4
bytesReadUpToNow=0
with open("C:\\outputs\\out.bin", mode='rb') as file: # b is important -> binary
    fileContent = file.read()   
    TimeS = struct.unpack("Q", fileContent[bytesReadUpToNow:bytesReadUpToNow+tsSize])[0]
    bytesReadUpToNow+=tsSize
    dt =datetime.datetime.fromtimestamp(TimeS/1000.0)
    rows, columns = struct.unpack("ii", fileContent[bytesReadUpToNow:bytesReadUpToNow+rowSize+columnSize])
    bytesReadUpToNow=bytesReadUpToNow+rowSize+columnSize
    data = struct.unpack("h" * (rows*columns), fileContent[bytesReadUpToNow:rows*columns*2+bytesReadUpToNow])[0]
    print(sys.getsizeof(data))
    print(type(data))

有没有办法在python中预定义数组的大小?

您可以使用“ bytearray”:

ba=bytearray(1000)      # zero-filled                                                 

In:  sys.getsizeof(ba)                                                       
Out: 1057

In:  ba[0:10]                                                                
Out: bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

In:  ba[5]=255                                                              

In:  ba[0:10]                                                               
Out: bytearray(b'\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00')

编辑:

with open("data","wb") as ff: 
     ff.write(int.to_bytes(100,1,"big")) 
     ff.write(int.to_bytes(300,2,"big")) 
     ff.write(int.to_bytes(5001,4,"big")) 

ba=bytearray(7)
ba
Out: bytearray(b'\x00\x00\x00\x00\x00\x00\x00')

with open("data","rb") as ff: 
     ba[0:7]= ff.read(7) 

ba                                          
Out: bytearray(b'd\x01,\x00\x00\x13\x89')

int.from_bytes(ba[0:1],"big")
Out: 100

int.from_bytes(ba[1:3],"big")
Out: 300

int.from_bytes(ba[3:],"big") 
Out: 5001

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM