Python to implement binary file formats?

I often have to write code to interact with binary file formats, for which there are no existing tools. I am looking for an easy way to implement readers / writers for structured binary formats - ideally this will allow me to create a reader using some simple declarative format.

I found the Construct module that works, but the author apparently was pretty much left by the author. I wonder if there are alternatives that people worked with.

+7
source share
2 answers

Personally, I would use the bitstring module, but I can be biased as I wrote it. As an example, you can find simple code for reading / writing binary format in the manual .

This is one way to create via binary format:

fmt = 'sequence_header_code, uint:12=horizontal_size_value, uint:12=vertical_size_value, uint:4=aspect_ratio_information, ... ' d = {'sequence_header_code': '0x000001b3', 'horizontal_size_value': 352, 'vertical_size_value': 288, 'aspect_ratio_information': 1, ... } s = bitstring.pack(fmt, **d) 

and one method for analyzing it:

 >>> s.unpack('bytes:4, 2*uint:12, uint:4') ['\x00\x00\x01\xb3', 352, 288, 1] 
+6
source

Take a look at the Hachoir .

+4
source

All Articles