[SciPy-user] Reading binary files

Bernardo Martins Rocha bernardo.rocha@meduni-graz...
Mon Jun 9 03:51:57 CDT 2008


I was trying to write a script in order to read a binary file. Then, I started looking for a python function to do it. First, I came up with scipy.io.fopen, which was OK. The only problem was that whenever I ran the script I got a message warning me that this class is deprecated, which is really annoying. 

Then I had to change to the new class for reading binary files: scipy.io.npfile

And it took me some time to change the script properly...so I'm posting what I did in order to describe my experience...and I'm open for suggestions that could improve this script.

----- fopen -------------------------------
from scipy.io.fopen import fopen

fh = fopen (filename,'r',fmt) # fmt is 'l' or 'B` (little or big endian)
header = fh.read(1024,'char')
slice_buf = fh.read(slice_size, dtype)

----- npfile -------------------------------
from scipy.io.npfile import npfile

fh = npfile (filename,'r',fmt)
dt = numpy.dtype('uint8')
header = fh.read_array(dt, 1024)
dt = numpy.dtype(dtype) # dtype = 'float32'
slice_buf = fh.read_array(dt, slice_size)

Bernardo M. Rocha

More information about the SciPy-user mailing list