[Numpy-discussion] problem reading binary data from file

Travis Oliphant oliphant@ee.byu....
Fri Apr 6 11:46:08 CDT 2007

Giorgio F. Gilestro wrote:

>Hello everyone.
>Here I go with my first problem for this ml!
>I am reading a long sequence of binary data from a file using a call
>similar to the following
>numpy.core.records.fromfile (filename, formats='i2', byteorder='big')
>My problem is that this function returns an array of tuples rather
>than an array of actual values.

You aren't getting an array of "tuples", but an array of "records."   
Each element of a record-array just displays as a tuple.

It sounds like you just want a regular array.  If that is the case, I'm 
not sure why you are using the records sub-class at all.   I'm also not 
clear on your use of the byteorder argument.  Are you reading big-endian 
data on a little-endian machine?  If you are always reading an writing 
in native format, then you don't need to specify the byte-order.

Once you have an array of records, you can easily view this as another 
kind of array.  Just use the .view method.  This does not copy any data 
but just gives you a different way of "looking" at the data.

In your case, a.view('i2') should do it.

Also, numpy.rec 

is short for numpy.core.records


More information about the Numpy-discussion mailing list