Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30870
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'string.': 0.04; '(even': 0.05; 'binary': 0.05; 'subject:skip:s 10': 0.05; 'rewrite': 0.07; 'received:mail-vc0-f174.google.com': 0.09; 'section,': 0.09; 'typeerror:': 0.09; 'writable': 0.09; 'resulting': 0.13; '(like': 0.15; 'file,': 0.15; 'sat,': 0.15; '"error': 0.16; '252': 0.16; 'encodings': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'given)': 0.16; 'oct': 0.16; 'ok!': 0.16; 'write,': 0.16; 'writes.': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'import': 0.21; '"",': 0.22; 'assuming': 0.22; 'finally,': 0.22; 'struct': 0.22; 'example': 0.23; 'work.': 0.23; 'elements': 0.23; 'header:In- Reply-To:1': 0.25; '(most': 0.27; 'handling': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'actual': 0.28; 'post': 0.28; 'arrays': 0.29; 'helpful.': 0.29; 'received:209.85.220.174': 0.29; 'seeks': 0.29; 'array': 0.29; 'character': 0.29; 'mode': 0.30; 'error': 0.30; 'file': 0.32; 'traceback': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'subject:with': 0.36; 'problems': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; "you've": 0.61; "you'll": 0.62; 'is.': 0.62; 'more': 0.63; 'skip:m 50': 0.65; 'subject:write': 0.84; 'inefficient': 0.91; 'write()': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=IRhmL2I73gRm5SrK8eTuZ8AhC2TjOkxvaiuIcGhNXiE=; b=y8aR4WeQ+ndiuP6ZzBMDb2b7f23c1TLkII+v61ky+2bVYGUjipfv2IogFkWKhExKXx i8Q4hCWIimGjIOKn37KdQ8yNqd0QvmziX21b3SnvaDb++7t7l+kI509Da+/s+LxpJZ10 nv+PwSvkvSJg9QRq95YDifekyRqn35ODBh76lLSumbPGK5j10QqwC3lcaGBFepNaAQdX dFmVfWeGna+4kIZkJyGDM41tBW6Z1cC7xt2AF17j6U9oQIDHPxK4m3bRuxX+YiJaOOXT Bv/hXVpqKxUizxUZULFT7xZY/lhJzrmWK6tSofO7EvILli0rngJKUkocKpGmvAiFDWe+ I6uA== |
| MIME-Version | 1.0 |
| In-Reply-To | <1349494056881-4991234.post@n6.nabble.com> |
| References | <1349494056881-4991234.post@n6.nabble.com> |
| Date | Sat, 6 Oct 2012 16:51:17 +1000 |
| Subject | Re: write binary with struct.pack_into |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1893.1349506280.27098.python-list@python.org> (permalink) |
| Lines | 42 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1349506280 news.xs4all.nl 6935 [2001:888:2000:d::a6]:43303 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:30870 |
Show key headers only | View raw
On Sat, Oct 6, 2012 at 1:27 PM, palmeira <palmeira@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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: write binary with struct.pack_into Chris Angelico <rosuav@gmail.com> - 2012-10-06 16:51 +1000
csiph-web