使用 Python 的 struct.unpack 时是否可以跳过字节?
Is it possible to skip over bytes when using Python's struct.unpack?
可以使用Python的struct.unpack to extract the useful values from a Bitmap file header,如下:
magic, file_size, _, _, data_offset = struct.unpack('<2sLHHL', file_header)
assert magic == 'BM'
有什么方法可以避免在这里分配给 _
(或另一个一次性变量)吗?是否可以更改格式字符串以使 struct.unpack
跳过两个未使用的 H
字段?
是的,使用"x"代码跳过1个字节。 (参见此处:https://docs.python.org/2/library/struct.html#format-characters)
即格式代码中的"H"替换为"xx"
可以使用Python的struct.unpack to extract the useful values from a Bitmap file header,如下:
magic, file_size, _, _, data_offset = struct.unpack('<2sLHHL', file_header)
assert magic == 'BM'
有什么方法可以避免在这里分配给 _
(或另一个一次性变量)吗?是否可以更改格式字符串以使 struct.unpack
跳过两个未使用的 H
字段?
是的,使用"x"代码跳过1个字节。 (参见此处:https://docs.python.org/2/library/struct.html#format-characters)
即格式代码中的"H"替换为"xx"