Reading bz2 file into numpy array

Peter Otten __peter__ at web.de
Mon Nov 22 05:37:22 EST 2010


Johannes Korn wrote:

> I tried:
> 
> from bz2 import *
> from numpy import *
> fd = BZ2File(filename, 'rb')
> read_data = fromfile(fd, float32)
> 
> but BZ2File doesn't seem to produce a transparent filehandle.

> is there a convenient way to read bz2 files into a numpy array?

Try

import numpy
import bz2

filename = ...

f = bz2.BZ2File(filename)
data = numpy.fromstring(f.read(), numpy.float32)

print data




More information about the Python-list mailing list