I'm trying to parse aa binary file. the file contains a few data packets each one starts with a timestamp then definition of an array (rows and columns each an int32)the the array itself. I've started with trying to parse a single packet but I have a problem with reading the array:
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))
Is there a way to predefined size of an array in python?
You can use "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')
Edit:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.