parse binary file in python?

Andreas Røsdal andrearo at stud.ntnu.no
Mon Jan 19 05:05:51 EST 2004


On Sun, 18 Jan 2004, "Martin v. Löwis" wrote:
> 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.

Thanks. Just wanting to know if I understood your answer correctly;
will the data variable above be an array of binary data,
eg. data[220] == 0, and data[221] == 1 etc?

Andreas Røsdal



More information about the Python-list mailing list