Sponsored Content
Top Forums Shell Programming and Scripting Python Binary File Read and Parse Post 302737639 by drd0spt on Friday 30th of November 2012 10:26:16 AM
Old 11-30-2012
Question [SOLVED] Python Binary File Read and Parse

Hi to everyone Smilie, i have a challenge right now in python that for now needs a bit of help in one part of the c0de.
The task is create a new file with the name of the file defined by the ASCII content between the 3 byte and the 16 byte that is parsed from the binary file, the file is over 20 Mb i need to create a file for each record ( each record contains the len of 820 ), so right now i know how to read it and do something with it, like :

Code:
Code:
#!/usr/bin/python

with open("file", "rb") as f:
	byte = f.read()
	if byte > 820:
	    print "Reach the 1 record mark on the File you have defined "

Now the problem is , how do i get the name file that i want that is the string between the 2 byte and the 16 byte and how to save every 820 bytes to a new file like :

Code:
#attention noob code coming for quick understading i have a for loop for #example not for use i have to use it for the each 820 bytes new file
#with the read of the 820 bytes on the new file and save it and go for the
#next 820 bytes and so on until the end of the file.
for f.read(820) in file:
    if f.read() = 820:
       a = f.read()
       b = open("Iam_from_2_to_16_byte_string", w)
       b.write(a)
       b.close

---------- Post updated 30-11-12 at 10:26 AM ---------- Previous update was 29-11-12 at 10:42 AM ----------

here is the solution :
Code:
n = 820
with open('/home/drdos/work/file.PDA', "rb") as f:
    while True:
        data = f.read(n)
        if not data:
            break
        
        filename = str(data[16:32])
        
        x = open(filename, 'wb').write(data)

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read and parse the content of csv file containing # as delimeter into fields using Bash?

#!/bin/bash i=0 cat 1.csv | while read fileline do echo "$fileline" IFS="#" flds=( $fileline ) nrofflds=${#flds} echo "noof fields$nrofflds" fld=0 while do echo "noof counter$fld" echo "$nrofflds" #fld1="${flds}" trying to store the content of line to fields but i... (4 Replies)
Discussion started by: barani75
4 Replies

2. Shell Programming and Scripting

Bash Script to read a file and parse each record

Hi Guys, I am new to unix scripting and I am tasked to parse through a CSV file delimited by #. Sample: sample.csv H#A#B#C D#A#B#C T#A#B#C H = Header D = Detail Record T = Tail What I need is to read the file and parse through it to get the columns. I have no idea on how... (8 Replies)
Discussion started by: 3vilwyatt
8 Replies

3. Shell Programming and Scripting

Help w/ script to read file and parse log message

Hi, I am working on the script to parsing the specific message like "aaaa" in multiple log files like N1-***,N2-***,N3-***... The script is to find the list of lof files which contains the message "aaaa" and export the list into excel filE. Can anyone give help? Thanks (2 Replies)
Discussion started by: shyork2001
2 Replies

4. Shell Programming and Scripting

Python script help. Kill binary on timeout

This is part of my script, as you can see when the alarm is raised it shows some text, what I need it to do is to actually kill the binary "test" (TEST_PATH) when the timeout happens. #!/usr/bin/python26 from optparse import OptionParser import nagios import re import subprocess... (0 Replies)
Discussion started by: erick_tuk
0 Replies

5. Shell Programming and Scripting

python - wget xml doc and parse with awk

Well, that's what I'd do in bash :) Here's what I have so far: import urllib2 from BeautifulSoup import BeautifulStoneSoup xml = urllib2.urlopen('http://weatherlink.com/xml.php?user=blah&pass=blah') soup = BeautifulStoneSoup(xml) print soup.prettify() but all it does is grab the html... (0 Replies)
Discussion started by: unclecameron
0 Replies

6. Shell Programming and Scripting

Read multiple files, parse data and append to a file

Hi..Can anyone suggest a simple way of achieving this. I have several files which ends with extension .vcf . I will give example with two files In the below files, we are interested in File 1: 38 107 C 3 T 6 C/T 38 241 C 4 T 5 C/T 38 247 T 4 C 5 T/C 38 259 T 3 C 6 T/C... (8 Replies)
Discussion started by: empyrean
8 Replies

7. UNIX for Advanced & Expert Users

How to parse a binary file?

Hello to all in forum, Please your help with this. I have a binary file that is represented in Binary or Binary Coded Decimal (BCD). Do you know a parser for this kind of binary files? I have the structure format, but I don't know how and where to begin. You answer would be very... (15 Replies)
Discussion started by: Ophiuchus
15 Replies

8. Programming

C program to read a binary file and search for a string?

Hi, I am not a C programmer. The only C exposure I have is reading and completing the exercises from the C (ANSI C ) Programming Language book:o At the moment, I am using the UNIX strings command to extract information for a binary file and grepping for a particular string and the value... (3 Replies)
Discussion started by: newbie_01
3 Replies

9. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

10. Shell Programming and Scripting

Need python script to read a file

more data.txt user1:psw1:Moni,Admi user2:psw2:Depl,Moni,Admi I wish to perform the following task on the data.txt in my python script 1. read line by line data.txt 2. Once you read the first line split it using delimiter ":" i.e "user1" "psw1" & "Moni,Admi" 3. split the 3rd string i.e... (4 Replies)
Discussion started by: mohtashims
4 Replies
PYTHON-CONFIG(1)					      General Commands Manual						  PYTHON-CONFIG(1)

NAME
python-config - output build options for python C/C++ extensions or embedding SYNOPSIS
python-config [ --prefix ] [ --exec-prefix ] [ --includes ] [ --libs ] [ --cflags ] [ --ldflags ] [ --help ] DESCRIPTION
python-config helps compiling and linking programs, which embed the Python interpreter, or extension modules that can be loaded dynamically (at run time) into the interpreter. OPTIONS
--cflags print the C compiler flags. --ldflags print the flags that should be passed to the linker. --includes similar to --cflags but only with -I options (path to python header files). --libs similar to --ldflags but only with -l options (used libraries). --prefix prints the prefix (base directory) under which python can be found. --exec-prefix print the prefix used for executable program directories (such as bin, sbin, etc). --help print the usage message. EXAMPLES
To build the singe-file c program prog against the python library, use gcc $(python-config --cflags --ldflags) progr.cpp -o progr.cpp The same in a makefile: CFLAGS+=$(shell python-config --cflags) LDFLAGS+=$(shell python-config --ldflags) all: progr To build a dynamically loadable python module, use gcc $(python-config --cflags --ldflags) -shared -fPIC progr.cpp -o progr.so SEE ALSO
python (1) http://docs.python.org/extending/extending.html /usr/share/doc/python/faq/extending.html AUTHORS
This manual page was written by Johann Felix Soden <johfel@gmx.de> for the Debian project (and may be used by others). November 27, 2011 PYTHON-CONFIG(1)
All times are GMT -4. The time now is 09:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy