write binary with struct.pack_into

Discussion in 'Python' started by palmeira, Oct 6, 2012.

  1. palmeira

    palmeira Guest

    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.
     
    palmeira, Oct 6, 2012
    #1
    1. Advertising

  2. 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.
     
    88888 Dihedral, Oct 6, 2012
    #2
    1. Advertising

  3. 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.
     
    88888 Dihedral, Oct 6, 2012
    #3
  4. 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
     
    Alexander Blinne, Oct 6, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tzury Bar Yochay
    Replies:
    4
    Views:
    594
    John Machin
    Sep 21, 2008
  2. Hans Müller
    Replies:
    1
    Views:
    573
    Emile van Sebille
    Mar 19, 2010
  3. Dennis Lee Bieber

    Re: write binary with struct.pack_into

    Dennis Lee Bieber, Oct 6, 2012, in forum: Python
    Replies:
    2
    Views:
    185
    Chris Angelico
    Oct 6, 2012
  4. Chris Angelico

    Re: write binary with struct.pack_into

    Chris Angelico, Oct 6, 2012, in forum: Python
    Replies:
    0
    Views:
    224
    Chris Angelico
    Oct 6, 2012
  5. Peter Otten

    Re: write binary with struct.pack_into

    Peter Otten, Oct 6, 2012, in forum: Python
    Replies:
    0
    Views:
    183
    Peter Otten
    Oct 6, 2012
Loading...

Share This Page