Hi, Is there a function similar to numarray's fromfile, to read a binary array from a file ? What is the best way to read such a file into a Numeric array. thanks, ~Hari
On Mon, Aug 22, 2005 at 01:27:55PM -0400, Hari Sundar wrote:
Hi,
Is there a function similar to numarray's fromfile, to read a binary array from a file ? What is the best way to read such a file into a Numeric array.
It depends on the format of the file. If it's a pickled Numeric array, then the pickle module can read it back. Otherwise, you get to read the file (by opening it first and calling its read() method) and you can use the fromstring() function in numeric. However you need to know the type of the array and its shape. -- Alexandre Fayolle LOGILAB, Paris (France). http://www.logilab.com http://www.logilab.fr http://www.logilab.org
On Tue, 23 Aug 2005, Alexandre Fayolle wrote:
On Mon, Aug 22, 2005 at 01:27:55PM -0400, Hari Sundar wrote:
Hi,
Is there a function similar to numarray's fromfile, to read a binary array from a file ? What is the best way to read such a file into a Numeric array.
Another option is to use scipy.io, e.g.: In [5]:scipy.io.fread? Type: builtin_function_or_method Base Class: <type 'builtin_function_or_method'> String Form: <built-in function fread> Namespace: Interactive Docstring: g = numpyio.fread( fid, Num, read_type { mem_type, byteswap}) fid = open file pointer object (i.e. from fid = open('filename') ) Num = number of elements to read of type read_type read_type = a character in 'cb1silfdFD' (PyArray types) describing how to interpret bytes on disk. OPTIONAL mem_type = a character (PyArray type) describing what kind of PyArray to return in g. Default = read_type byteswap = 0 for no byteswapping or a 1 to byteswap (to handle different endianness). Default = 0. I use this a lot and it works very well. For example: from scipy import * # --- Create some arrays: x=arange(10)/10.0 z=zeros( (5,5),"d") z_complex=zeros( (5,7),"D") # --- write them as binary data: fp=file("x.dat","wb") io.fwrite(fp,len(x),x) fp.close() fp=file("z.dat","wb") io.fwrite(fp,z.shape[0]*z.shape[1],z) fp.close() fp=file("z_complex.dat","wb") io.fwrite(fp,z_complex.shape[0]*z_complex.shape[1],z_complex) fp.close() # --- and read them back fp=file("x.dat","rb") x_read=io.fread(fp,10,"d") fp.close() fp=file("z.dat","rb") z_read=reshape(io.fread(fp,5*5,"d"),(5,5)) fp.close() fp=file("z_complex.dat","rb") z_complex_read=reshape(io.fread(fp,5*7,"D"),(5,7)) fp.close() Remark: `"wb"` and `"rb"` is only needed under Windows, normally `"w"` and `"r"` would be enough. Best, Arnd
Arnd Baecker wrote: and bunch of good stuff, and
... fp=file("x.dat","wb") io.fwrite(fp,len(x),x) fp.close() ... # --- and read them back fp=file("x.dat","rb") x_read=io.fread(fp,10,"d") fp.close()
Remark: `"wb"` and `"rb"` is only needed under Windows, normally `"w"` and `"r"` would be enough.
This is only true if you are talking about Unix/Linux vs. Windows. There are other systems where the 'b' is necessary, and even where it makes no difference it explicitly states your intent. So, I'd always use "wb" or "rb" in these cases. On some filesystems, (VMS comes to mind) a text file is a completely different format from a binary file. --Scott David Daniels Scott.Daniels@Acm.Org
participants (4)
-
Alexandre Fayolle
-
Arnd Baecker
-
Hari Sundar
-
Scott David Daniels