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
DH_PYTHON(1)							     Debhelper							      DH_PYTHON(1)

NAME
dh_python - calculates Python dependencies and adds postinst and prerm Python scripts (deprecated) SYNOPSIS
dh_python [debhelperoptions] [-n] [-V version] [moduledirs...] DESCRIPTION
Note: This program is deprecated. You should use dh_pysupport or dh_pycentral instead. This program will do nothing if debian/pycompat or a Python-Version control file field exists. dh_python is a debhelper program that is responsible for generating the ${python:Depends} substitutions and adding them to substvars files. It will also add a postinst and a prerm script if required. The program will look at Python scripts and modules in your package, and will use this information to generate a dependency on python, with the current major version, or on pythonX.Y if your scripts or modules need a specific python version. The dependency will be substituted into your package's control file wherever you place the token ${python:Depends}. If some modules need to be byte-compiled at install time, appropriate postinst and prerm scripts will be generated. If already byte- compiled modules are found, they are removed. If you use this program, your package should build-depend on python. OPTIONS
module dirs If your package installs Python modules in non-standard directories, you can make dh_python check those directories by passing their names on the command line. By default, it will check /usr/lib/site-python, /usr/lib/$PACKAGE, /usr/share/$PACKAGE, /usr/lib/games/$PACKAGE, /usr/share/games/$PACKAGE and /usr/lib/python?.?/site-packages. Note: only /usr/lib/site-python, /usr/lib/python?.?/site-packages and the extra names on the command line are searched for binary (.so) modules. -V version If the .py files your package ships are meant to be used by a specific pythonX.Y version, you can use this option to specify the desired version, such as 2.3. Do not use if you ship modules in /usr/lib/site-python. -n, --noscripts Do not modify postinst/prerm scripts. CONFORMS TO
Debian policy, version 3.5.7 Python policy, version 0.3.7 SEE ALSO
debhelper(7) This program is a part of debhelper. AUTHOR
Josselin Mouette <joss@debian.org> most ideas stolen from Brendan O'Dea <bod@debian.org> 8.9.0ubuntu2.1 2012-06-12 DH_PYTHON(1)
All times are GMT -4. The time now is 06:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy