473,545 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9766
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("binaryfil e.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("binaryfil e.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("binaryfil e.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
15201
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 pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large,...
19
2443
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 ###,1,val_1,2,val_2,3,val_3,5,val_5,11,val_11,25,val_25,967,val_967 In other words, different layouts (defined mostly by what is in val_1, val_2, val_3). The ,#,...
7
6473
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 only takes a string argument anyway. Is there a way to actually store integers in a file, so that they can be read and used (added, compared, etc.)...
34
2936
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 the executable ? Is it possible ? Also, how does the compiler add inling to the program ? I know that whenever it sees"inline" in front of the...
13
4164
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 from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any...
6
8543
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 circumstances(XML file source) and with exactly the same value it gets it perfectly correct all the time. These are the results I got, XML is...
4
2914
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. Unfortunately, I am not an SQL guru and was hoping Python could help. Any pointers are appreciated. Thanks!
5
64596
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts,...
1
64027
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts,...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.