Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'received:209.85.223': 0.03; 'binary': 0.05; 'modify': 0.05; 'subject:skip:s 10': 0.05; 'python': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'archive': 0.11; '252': 0.16; 'context:': 0.16; 'gonna': 0.16; 'ok!': 0.16; 'file.': 0.20; 'import': 0.21; 'struct': 0.22; 'cc:2**0': 0.23; 'elements': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'values': 0.26; 'oop': 0.29; 'pickle': 0.29; 'array': 0.29; "i'm": 0.29; 'error': 0.30; 'url:python': 0.32; 'file': 0.32; 'computing': 0.32; 'extract': 0.33; 'problem': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'list': 0.35; 'from:addr:googlemail.com': 0.35; 'sometimes': 0.35; 'received:209.85': 0.35; 'subject:with': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'different': 0.63; 'information,': 0.63; 'skip:m 50': 0.65; 'url:6': 0.65; 'dear': 0.66; 'middle': 0.66; 'heavy': 0.83; 'subject:write': 0.84; '8bit%:70': 0.91 Newsgroups: comp.lang.python Date: Fri, 5 Oct 2012 21:39:09 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 123.192.32.215 MIME-Version: 1.0 Subject: Re: write binary with struct.pack_into From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349498359 news.xs4all.nl 6971 [2001:888:2000:d::a6]:50603 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30865 palmeira=E6=96=BC 2012=E5=B9=B410=E6=9C=886=E6=97=A5=E6=98=9F=E6=9C=9F=E5= =85=ADUTC+8=E4=B8=8A=E5=8D=8811=E6=99=8227=E5=88=8647=E7=A7=92=E5=AF=AB=E9= =81=93=EF=BC=9A > Dear pythonists, >=20 >=20 >=20 > I'm having a problem with read/write binary in python. >=20 > I have a binary file that I need to read information, extract a array, >=20 > modify this array and put these values into file again in same binary >=20 > format. >=20 > I need to use unpack_from and pack_into because sometimes gonna need >=20 > read/write in the middle of file. >=20 >=20 >=20 > Script: >=20 >=20 >=20 > import struct >=20 > bloco=3D'>%df' %(252) #Binary format >=20 >=20 >=20 > # READ >=20 > fa=3Dopen('testIN.bin') >=20 > my_array=3Dstruct.unpack_from(bloco,fa.read()[0*4:251*4]) # my_aray = =3D 252 >=20 > elements array >=20 > ## This read is OK! >=20 >=20 >=20 > #WRITE >=20 > fb=3Dopen('testOUT.bin') >=20 > test=3Dstruct.pack_into(bloco,fb.write()[0*4:251*4]) # ERROR in this WRI= TE >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 > Regards, >=20 >=20 >=20 > Ronaldo Palmeira. >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 > -- >=20 > View this message in context: http://python.6.n6.nabble.com/write-binary-= with-struct-pack-into-tp4991234.html >=20 > Sent from the Python - python-list mailing list archive at Nabble.com. Are you writing and reading files produce by different=20 languages? The pickle part is better for OOP and glue logics in python. The heavy computing part should be done in CYTHON.