473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

parse binary file in python?

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.
Jul 18 '05 #1
5 9757
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

Jul 18 '05 #2
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

Jul 18 '05 #3
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
Jul 18 '05 #4
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
Jul 18 '05 #5
Andreas Røsdal <an******@stud.ntnu.no> writes:
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?


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

--
To summarise the summary of the summary:- people are a problem.
-- The Hitch-Hikers Guide to the Galaxy, Episode 12
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
19
by: Peter A. Schott | last post by:
I've got a file that seems to come across more like a dictionary from what I can tell. Something like the following format: ###,1,val_1,2,val_2,3,val_3,5,val_5,10,val_10...
7
by: John Salerno | last post by:
In C#, writing to a binary file wrote the actual data types into the file (integers, etc.). Is this not how Python binary files work? I tried to write integers into a file, but the write method...
34
by: priyanka | last post by:
Hi, I was wondering if we could parse or do something in the executable( whose source language was C). How can I use some scripting language like perl/python to find out the information about...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
4
by: kyosohma | last post by:
Hi All, Can Python parse a trace file created with MS SQL's profiler? There are a few thousand lines in the trace file and I need to find the insert statements and the stored procedures....
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.