Newbie Q: (NumPy) how to write an array in a file ?

Tony Sideris tonys111 at erols.com
Tue Oct 10 11:59:53 EDT 2000


"Nicolas Decoster" <Nicolas.Decoster at Noveltis.fr> wrote in message
news:39E33793.ADEDAAEC at Noveltis.fr...
> Hi !
>
> First, I must say that it is my first steps in Python and NumPy.
>
> I want to write an array in a file in binary format. Is there something
> as simple and as _fast_ as 'fwrite' from C ?
>
> I don't find anything like that in the documentation. I need something
> that is portable, so I think that pickle will not work.
>
> Thanks for help.
>
> Nicolas.
>
> --
> Tél. : 00 (33) 5 62 88 11 16
> Fax  : 00 (33) 5 62 88 11 12
> Nicolas.Decoster at Noveltis.fr
>
> Noveltis
> Parc Technologique du Canal
> 2, avenue de l'Europe
> 31520 Ramonville Saint Agne - France

use the 'struct' module with pack for writing, and unpack for reading for
instance.

##    Untested
import struct;
file = open('file', 'wb'); ## open output file for binary write
file.write(struct.pack('bb', 0, 1)); ## write 0 and 1 to file (in bytes)
file.close();

to read the file back in use file.read() and struct.unpack()

-Tony






More information about the Python-list mailing list