473,614 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary File Parsing and Creation of new Files

2 New Member
I have a file ( size : 20 mb | binary file ) that needs to be parsed every 820 bytes and that very content of 820 saved into a new file with the name of the file being the string(ASCII) between the 2byte and the 16byte mark.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
^ THE FILENAME COMES FROM HERE ^

Ok now that the challenge is explained ( i hope ) what i do have for now is this :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. with open("file", "rb") as f:
  4.     byte = f.read()
  5.     if byte > 820:
  6.         print "Reach the 1 record mark on the File you have defined "

Hmmm i don't know how to iterate every time i read the buffer and the f.read reaches 820 bytes and save it to a new file every 820bytes then the next 820bytes and continue to the end of the file .
Nov 30 '12 #1
3 2975
bvdet
2,851 Recognized Expert Moderator Specialist
The io module is suitable for reading raw bytes. Read 840 bytes and save to an identifier. Write the 840 bytes out using a slice of the saved string (s[2:17] in your case) as the file name. Here's a simple example of using io.FileIO:
Expand|Select|Wrap|Line Numbers
  1. import io
  2. f = io.FileIO(file_name)
  3. while True:
  4.     s = f.read(10)
  5.     if not s:
  6.         break
  7.     print "******"
  8.     print s
  9.     print s[2:7]
  10.     print "******"
  11. f.close()
Nov 30 '12 #2
drd0s
2 New Member
nice i made after this my way that was like this :
Expand|Select|Wrap|Line Numbers
  1. n = 820
  2. with open('/home/drdos/work/file.PDA', "rb") as f:
  3.     while True:
  4.         data = f.read(n)
  5.         if not data:
  6.             break
  7.  
  8.         filename = str(data[16:32])
  9.  
  10.         x = open(filename, 'wb').write(data)
Nov 30 '12 #3
bvdet
2,851 Recognized Expert Moderator Specialist
One thing you left off:
Expand|Select|Wrap|Line Numbers
  1.         x.close()
That ensures the data will be flushed to disk and you are not left with an open file.

Maybe you should use a try/except block in case filename has characters unsuitable for a file name.
Nov 30 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3574
by: Marian Jancar | last post by:
Hi, Is there a module for parsing spec files available? Marian -- -- Best Regards,
0
978
by: stephane ancelot | last post by:
Hi, I am new to python and I would like to know if there are some ways to provide a binary file as target . Because my projects may contain lot of py files and I do not want to provide source files . for maintenance puproses it is too easier for me to send only one file to my customer. Best Regards Steph
5
9773
by: Andreas Røsdal | last post by:
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.
5
5306
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the quotes), and also some more text like this and file name (e.g. "Content-Disposition: form-data; name="upload_file"; filename="C:\testing\myfile.da<WBR>­t" Content-Type: application/octet-stream")
1
2457
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement and drawing instructions like "move to's" and "change color's" optionally followed by one or more numeric (int or float) arguments. My problem is that the parsing algorithm I've currently implemented is extremely slow.
1
2573
by: young_leaf | last post by:
Hi, What is the most efficient way of making a Binary File Reader, which reads binary file on command of other application. The program: a) Read Binary File b) Create a Table of data based on the "data" written on the Binary File c) The Application has access to a DLL which contain functions d) a specified "data" from the Table is passed as parameter(s) of the
68
5205
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
8
1935
by: Bryan.Fodness | last post by:
Hello, I am having trouble writing the code to read a binary string. I would like to extract the values for use in a calculation. Any help would be great. Here is my function that takes in a string. def parseSequence(data, start):
27
5089
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type; /* 0 Type */ unsigned char sn; /* 1-3 Serial Number */
4
1900
by: Tom | last post by:
I am trying to update another developers code, and am stuck in a cludge. It works like this: A user uploads a file from the web page. Our code reads from a NetworkStream to a Filestream, and writes out a temporary file. Then the file is parsed (The file contains HTML data + the actual binary file that was transmitted). As part of the parsing, once the main binary file is to be parsed out, it is written with a new Filestream,
0
8185
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8132
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8630
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8285
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8436
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5542
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4129
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2570
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
1429
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.