我正试图使用python在一个长的二进制文件中循环,该文件充满了8字节的记录。
每个记录都有格式
[ uint16 | uint16 | uint32 ]
(这是
"HHI"
在结构格式中)
显然每个8字节块都被当作
int
,而不是8字节的数组,然后导致
struct.unpack
调用失败
with open(fname, "rb") as f:
sz=struct.calcsize("HHI")
print(sz) # This shows 8, as expected
for raw in f.read(sz): # Expect this should read 8 bytes into raw
print(type(raw)) # This says raw is an 'int', not a byte-array
record=struct.unpack("HHI", raw ) # "TypeError: a bytes-like object is required, not 'int'"
print(record)
如何将文件作为一系列结构读取并分别打印出来?