Quantcast

write binary with struct.pack_into

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

write binary with struct.pack_into

palmeira
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: write binary with struct.pack_into

Thomas Kluyver-2
Hi Ronaldo,

This list is actually about IPython, an interactive Python shell. It
sounds like your question is about using Python in general. In future,
the programming Q&A site Stackoverflow is a good place for this kind
of question: http://stackoverflow.com/questions/tagged/python

On 6 October 2012 04:33, palmeira <[hidden email]> wrote:
> test=struct.pack_into(bloco,fb.write()[0*4:251*4])  # ERROR in this WRITE

This is your problem, though. It should look more like:
struct.pack_into(bloco, fb, 0, my_array)

The docs are here: http://docs.python.org/library/struct.html#struct.pack_into

Best wishes,
Thomas
_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: write binary with struct.pack_into

palmeira
Thanks for your help and sorry about the list messy

sds,

Ronaldo Maia de Jesus Palmeira
Doutorando em Engenharia Oceânica 
Grupo de Processos Oceânicos - GruPO
[hidden email]  |  [hidden email]
UFRJ | Universidade Federal do Rio de Janeiro

"A percepção do desconhecido é a mais fascinante das experiências. O homem que não tem os olhos abertos para o misterioso passará pela vida sem ver nada."
Albert Einstein



On Sat, Oct 6, 2012 at 3:37 PM, Thomas Kluyver-2 [via Python] <[hidden email]> wrote:
Hi Ronaldo,

This list is actually about IPython, an interactive Python shell. It
sounds like your question is about using Python in general. In future,
the programming Q&A site Stackoverflow is a good place for this kind
of question: http://stackoverflow.com/questions/tagged/python


On 6 October 2012 04:33, palmeira <[hidden email]> wrote:
> test=struct.pack_into(bloco,fb.write()[0*4:251*4])  # ERROR in this WRITE

This is your problem, though. It should look more like:
struct.pack_into(bloco, fb, 0, my_array)

The docs are here: http://docs.python.org/library/struct.html#struct.pack_into

Best wishes,
Thomas
_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user



If you reply to this email, your message will be added to the discussion below:
http://python.6.n6.nabble.com/write-binary-with-struct-pack-into-tp4991236p4991292.html
To start a new topic under IPython - User, email [hidden email]
To unsubscribe from write binary with struct.pack_into, click here.
NAML

Loading...