Skipping bytes while reading a binary file?

Slaunger Slaunger at gmail.com
Fri Feb 6 08:20:08 EST 2009


You might also want to have a look at a numpy memmap viewed as a
recarray.

from numpy import dtype, memmap, recarray
# Define your record in the file, 4bytes for the real value,
# and 4 bytes for the imaginary (assuming Little Endian repr)
descriptor = dtype([("r", "<f4"), ("i", "<f4")])
# Now typecast a memap (a memory efficient array) onto the file in
read only mode
# Viewing it as a recarray means the attributes data.r and data.i are
acessible as
# ordinary numpy array
data = memmap(filename, dtype=descriptor, mode='r').view(recarray)
print "First 100 real values:", data.r[:100]

--Slaunger



More information about the Python-list mailing list