Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30862 > unrolled thread
| Started by | palmeira <palmeira@gmail.com> |
|---|---|
| First post | 2012-10-05 20:27 -0700 |
| Last post | 2012-10-06 17:55 +0200 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
write binary with struct.pack_into palmeira <palmeira@gmail.com> - 2012-10-05 20:27 -0700
Re: write binary with struct.pack_into 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-05 21:39 -0700
Re: write binary with struct.pack_into 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-05 21:39 -0700
Re: write binary with struct.pack_into Alexander Blinne <news@blinne.net> - 2012-10-06 17:55 +0200
| From | palmeira <palmeira@gmail.com> |
|---|---|
| Date | 2012-10-05 20:27 -0700 |
| Subject | write binary with struct.pack_into |
| Message-ID | <mailman.1889.1349494066.27098.python-list@python.org> |
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.
[toc] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-10-05 21:39 -0700 |
| Message-ID | <161b9212-9e78-4e88-b6dc-98a8b5cb285e@googlegroups.com> |
| In reply to | #30862 |
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.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-10-05 21:39 -0700 |
| Message-ID | <mailman.1890.1349498359.27098.python-list@python.org> |
| In reply to | #30862 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-10-06 17:55 +0200 |
| Message-ID | <50705485$0$6629$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #30862 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web