Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
write binary with struct.pack_into
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
palmeira  
View profile  
 More options Oct 5 2012, 11:27 pm
Newsgroups: comp.lang.python
From: palmeira <palme...@gmail.com>
Date: Fri, 5 Oct 2012 20:27:36 -0700 (PDT)
Local: Fri, Oct 5 2012 11:27 pm
Subject: write binary with struct.pack_into
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-tp49...
Sent from the Python - python-list mailing list archive at Nabble.com.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Oct 6 2012, 12:39 am
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Fri, 5 Oct 2012 21:39:09 -0700 (PDT)
Local: Sat, Oct 6 2012 12:39 am
Subject: Re: write binary with struct.pack_into
palmeira於 2012年10月6日星期六UTC+8上午11時27分47秒寫道:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Oct 6 2012, 12:39 am
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Fri, 5 Oct 2012 21:39:09 -0700 (PDT)
Local: Sat, Oct 6 2012 12:39 am
Subject: Re: write binary with struct.pack_into
palmeira於 2012年10月6日星期六UTC+8上午11時27分47秒寫道:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 6 2012, 2:51 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sat, 6 Oct 2012 16:51:17 +1000
Local: Sat, Oct 6 2012 2:51 am
Subject: Re: write binary with struct.pack_into

On Sat, Oct 6, 2012 at 1:27 PM, palmeira <palme...@gmail.com> wrote:
> 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

You have a beautiful parallel here, but I think that's where your
problem is. In the READ section, you have fa.read() which will read
the whole file, and then you slice the resulting string. That's pretty
inefficient for large files, but it'll work.

But when you write, that completely does not work. (Even assuming
you've opened read/write, per Dennis's comment.)

>>> fb.write()

Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    fb.write()
TypeError: write() takes exactly 1 argument (0 given)

It needs an argument of what to write out, it doesn't return a
writable buffer suitable for pack_into.

I recommend you completely rewrite your file handling to use actual
seeks and file writes. Also, you'll want (I think) to use binary mode
on your files; character encodings don't mean much when you're working
with arrays of numbers.

Finally, when you post issues, "ERROR in this WRITE" isn't very
helpful. Please please post the full traceback (like in my trivial
example above), as problems will be much more visible.

Hope that's of some value!

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 6 2012, 3:45 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Sat, 06 Oct 2012 09:45:03 +0200
Local: Sat, Oct 6 2012 3:45 am
Subject: Re: write binary with struct.pack_into

palmeira wrote:
> 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.

Use pack/unpack and file.seek() instead.

> 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

However, I think you have picked the wrong API. So:

# untested

import sys
import array

offset = 0
N = 252

a = array.array("f")
with open("testIN.bin", "rb") as f:
    f.seek(offset)
    a.read(f, N)
if sys.byteorder == "little":
    a.byteswap()

# process a

if sys.byteorder == "little":
    a.byteswap()
with open("testOUT.bin", "wb") as f:
    f.seek(offset)
    a.write(f)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Grant Edwards  
View profile  
 More options Oct 6 2012, 9:26 am
Newsgroups: comp.lang.python
From: Grant Edwards <inva...@invalid.invalid>
Date: Sat, 6 Oct 2012 13:26:44 +0000 (UTC)
Local: Sat, Oct 6 2012 9:26 am
Subject: Re: write binary with struct.pack_into
On 2012-10-06, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:

> On Fri, 5 Oct 2012 20:27:36 -0700 (PDT), palmeira <palme...@gmail.com>
> declaimed the following in gmane.comp.python.general:

>> #WRITE
>> fb=open('testOUT.bin')

>    Unless you specify otherwise, open() defaults to read-only

It also defaults to 'text' mode which does cr/lf translaction.  That
will break both reads and writes on any binary file containing 0x0a
and 0x0d bytes.

--
Grant


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 6 2012, 9:52 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sat, 6 Oct 2012 23:52:47 +1000
Local: Sat, Oct 6 2012 9:52 am
Subject: Re: write binary with struct.pack_into

On Sat, Oct 6, 2012 at 11:26 PM, Grant Edwards <inva...@invalid.invalid> wrote:
> On 2012-10-06, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
>> On Fri, 5 Oct 2012 20:27:36 -0700 (PDT), palmeira <palme...@gmail.com>
>> declaimed the following in gmane.comp.python.general:

>>> #WRITE
>>> fb=open('testOUT.bin')

>>       Unless you specify otherwise, open() defaults to read-only

> It also defaults to 'text' mode which does cr/lf translaction.  That
> will break both reads and writes on any binary file containing 0x0a
> and 0x0d bytes.

And, in Python 3, it'll be returning Unicode characters too, unless
the decode fails. You definitely want binary mode.

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alexander Blinne  
View profile  
 More options Oct 6 2012, 11:55 am
Newsgroups: comp.lang.python
From: Alexander Blinne <n...@blinne.net>
Date: Sat, 06 Oct 2012 17:55:49 +0200
Local: Sat, Oct 6 2012 11:55 am
Subject: Re: write binary with struct.pack_into
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »