Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > write binary with struct.pack_into

Reply
Thread Tools

write binary with struct.pack_into

 
 
palmeira
Guest
Posts: n/a
 
      10-06-2012
Dear pythonists,

I'm having a problem with read/write binary in python.
I have a binary file that I need to read information, extract a array,
modify this array and put these values into file again in same binary
format.
I need to use unpack_from and pack_into because sometimes gonna need
read/write in the middle of file.

Script:

import struct
bloco='>%df' %(252) #Binary format

# READ
fa=open('testIN.bin')
my_array=struct.unpack_from(bloco,fa.read()[0*4:251*4]) # my_aray = 252
elements array
## This read is OK!

#WRITE
fb=open('testOUT.bin')
test=struct.pack_into(bloco,fb.write()[0*4:251*4]) # ERROR in this WRITE




Regards,

Ronaldo Palmeira.




--
View this message in context: http://python.6.n6.nabble.com/write-...tp4991234.html
Sent from the Python - python-list mailing list archive at Nabble.com.
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      10-06-2012
palmeira於 2012年10月6日星期*UTC+8上午11時27分47 寫道:
> Dear pythonists,
>
>
>
> I'm having a problem with read/write binary in python.
>
> I have a binary file that I need to read information, extract a array,
>
> modify this array and put these values into file again in same binary
>
> format.
>
> I need to use unpack_from and pack_into because sometimes gonna need
>
> read/write in the middle of file.
>
>
>
> Script:
>
>
>
> import struct
>
> bloco='>%df' %(252) #Binary format
>
>
>
> # READ
>
> fa=open('testIN.bin')
>
> my_array=struct.unpack_from(bloco,fa.read()[0*4:251*4]) # my_aray = 252
>
> elements array
>
> ## This read is OK!
>
>
>
> #WRITE
>
> fb=open('testOUT.bin')
>
> test=struct.pack_into(bloco,fb.write()[0*4:251*4]) # ERROR in this WRITE
>
>
>
>
>
>
>
>
>
> Regards,
>
>
>
> Ronaldo Palmeira.
>
>
>
>
>
>
>
>
>
> --
>
> View this message in context: http://python.6.n6.nabble.com/write-...tp4991234.html
>
> Sent from the Python - python-list mailing list archive at Nabble.com.


Are you writing and reading files produce by different
languages?

The pickle part is better for OOP and glue logics in python.

The heavy computing part should be done in CYTHON.
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      10-06-2012
palmeira於 2012年10月6日星期*UTC+8上午11時27分47 寫道:
> Dear pythonists,
>
>
>
> I'm having a problem with read/write binary in python.
>
> I have a binary file that I need to read information, extract a array,
>
> modify this array and put these values into file again in same binary
>
> format.
>
> I need to use unpack_from and pack_into because sometimes gonna need
>
> read/write in the middle of file.
>
>
>
> Script:
>
>
>
> import struct
>
> bloco='>%df' %(252) #Binary format
>
>
>
> # READ
>
> fa=open('testIN.bin')
>
> my_array=struct.unpack_from(bloco,fa.read()[0*4:251*4]) # my_aray = 252
>
> elements array
>
> ## This read is OK!
>
>
>
> #WRITE
>
> fb=open('testOUT.bin')
>
> test=struct.pack_into(bloco,fb.write()[0*4:251*4]) # ERROR in this WRITE
>
>
>
>
>
>
>
>
>
> Regards,
>
>
>
> Ronaldo Palmeira.
>
>
>
>
>
>
>
>
>
> --
>
> View this message in context: http://python.6.n6.nabble.com/write-...tp4991234.html
>
> Sent from the Python - python-list mailing list archive at Nabble.com.


Are you writing and reading files produce by different
languages?

The pickle part is better for OOP and glue logics in python.

The heavy computing part should be done in CYTHON.
 
Reply With Quote
 
Alexander Blinne
Guest
Posts: n/a
 
      10-06-2012
First, you should consider reading the documentation of
struct.unpack_from and struct.pack_into at
http://docs.python.org/library/struct.html quite carefully. It says,
that these commands take a parameter called offset, which names the
location of the data in a buffer (e.g. an opened file).

example:

bloco='>%df' % (252) # Format string (252 floats)
fa = open('testIN.bin', 'rb') # open for reading in binary mode
off = 0 # suppose i want to read block at beginning of file
my_array=struct.unpack_from(bloco, fa, off) #read data


now write them to another file:

fb = open('testOUT.bin', 'r+b') # open for updating in binary mode
off = 0 # suppose i want to write block at beginning of file
struct.pack_into(bloco, fb, off, *my_array) #write data to testOUT


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
x86 binary runs; x86_64 binary throws segfault Don C Programming 60 03-19-2010 05:58 AM
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
A 64-bit binary returning a value to a 32-bit binary? spammenotplui31@yahoo.ca C Programming 12 04-08-2007 07:02 AM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments