parse binary file in python?

"Martin v. Löwis" martin at v.loewis.de
Sun Jan 18 16:56:19 EST 2004


Andreas Røsdal wrote:
> I want to parse a binary file in python. Does
> python have some built in methods for doing this easily?
> Any links to example code would be nice..

Depends on the kind of parsing you want to do. Python
can naturally represent binary files in string objects,
e.g.

f = open("binaryfile.bin", "rb") # notice the b for binary mode
data = f.read()
f.close()

You can then look at the individual bytes by index. People
often use the struct and array modules to simplify processing.
Whether or not you would call this "parsing", I don't know
(for me, "parsing" implies presence of a context-free or
regular grammar of some kind).

Regards,
Martin




More information about the Python-list mailing list