|
From: Arnd B. <arn...@we...> - 2005-08-23 07:42:27
|
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 |