parse binary file in python?

  • Thread starter =?ISO-8859-1?Q?Andreas_R=F8sdal?=
  • Start date
?

=?ISO-8859-1?Q?Andreas_R=F8sdal?=

Hello,
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..
Thanks

Andreas R.
 
J

Jeff Epler

Python's primary tool for this is the "struct" module. See the stanard
library documentation for more info.

Also, remember always to open files in binary mode ("rb") so that people
running on windows don't get bizarre failures.

Jeff
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Andreas said:
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
 
M

Miki Tebeka

Hello Andreas,
I want to parse a binary file in python. Does
python have some built in methods for doing this easily?
Apart from struct and array module already suggested if you want
specific structures I recommend reading them in C and exposing the
interface using swig.

This way you can access structure members by name and not by offset.

HTH.
Miki
 
?

=?ISO-8859-1?Q?Andreas_R=F8sdal?=

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
 
M

Michael Hudson

Andreas Røsdal said:
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?

No, it's a string, but you can get what you want by using the `ord'
builtin function or the array module or the struct module (or probably
various other ways).

Cheers,
mwh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top