write binary with struct.pack_into

88888 Dihedral dihedral88888 at googlemail.com
Sat Oct 6 00:39:09 EDT 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-binary-with-struct-pack-into-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.



More information about the Python-list mailing list