user200783
user200783

Reputation: 14348

Is it possible to skip over bytes when using Python's struct.unpack?

It is possible to use Python's struct.unpack to extract the useful values from a Bitmap file header, as follows:

magic, file_size, _, _, data_offset = struct.unpack('<2sLHHL', file_header)
assert magic == 'BM'

Is there any way to avoid the need to assign to _ (or another throwaway variable) here? Is it possible to change the format string to make struct.unpack skip over the two unused H fields?

Upvotes: 6

Views: 2195

Answers (1)

Torben Klein
Torben Klein

Reputation: 3116

Yes, use the "x" code to skip 1 byte. (see here: https://docs.python.org/2/library/struct.html#format-characters)

I.e., replace "H" with "xx" in the format code.

Upvotes: 9

Related Questions