Python Binary File Read and Parse | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Python Binary File Read and Parse

Shell Programming and Scripting


Tags
binaries, parse lines onto new file, python, solved

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-30-2012
drd0spt drd0spt is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 30 November 2012, 10:41 AM EST
Posts: 21
Thanks: 4
Thanked 0 Times in 0 Posts
Question [SOLVED] Python Binary File Read and Parse

Hi to everyone , 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)

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read multiple files, parse data and append to a file empyrean Shell Programming and Scripting 8 09-27-2012 05:50 PM
Help to parse csv file with shell script Grhyll UNIX for Dummies Questions & Answers 4 01-19-2012 09:45 AM
Help w/ script to read file and parse log message shyork2001 Shell Programming and Scripting 2 04-06-2010 10:02 AM
Bash Script to read a file and parse each record 3vilwyatt Shell Programming and Scripting 8 02-25-2010 01:04 AM
How to read and parse the content of csv file containing # as delimeter into fields using Bash? barani75 Shell Programming and Scripting 4 02-22-2010 09:28 AM



All times are GMT -4. The time now is 03:49 PM.