struct.error: unpack requires a buffer of 26 bytes

with open('Test.bmp', 'rb') as f:
    s = f.read(30)

#利用struct提取信息
struct.unpack('<ccIIIIIHH',s)

#报错
#struct.error: unpack requires a buffer of 26 bytes

原因是,unpack函数的第一个参数中少写了一个I(4字节),导致处理的数据大小为26Bytes,而s为30Bytes。

修改为:

struct.unpack('<ccIIIIIIHH',s)
(b'B', b'M', 33510, 0, 54, 40, 109, 102, 1, 24)

总结:struct.unpack将二进制数据转化为其他数据时,第一个参数表示处理指令,其中处理的数据大小必须与第二个参数完全相等。

posted @ 2020-10-09 20:06  ShineLe  阅读(8945)  评论(0编辑  收藏  举报