Stéfan van der Walt wrote: > 2008/11/20 Ryan May <[EMAIL PROTECTED]>: >> Does anyone know why numpy.loadtxt(), in checking the validity of a >> filehandle, checks for the seek() method, which appears to have no >> bearing on whether an object will work? > > I think this is simply a naive mistake on my part. I was looking for > a way to identify files; your patch would be welcome.
I've attached a simple patch that changes the check for seek() to a check for readline(). I'll punt on my idea of just using iterators, since that seems like slightly greater complexity for no gain. (I'm not sure how many people end up with data in a list of strings and wish they could pass that to loadtxt). While you're at it, would you commit my patch to add support for bzipped files as well (attached)? Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma
Index: numpy/lib/io.py =================================================================== --- numpy/lib/io.py (revision 5953) +++ numpy/lib/io.py (working copy) @@ -253,8 +253,8 @@ Parameters ---------- fname : file or string - File or filename to read. If the filename extension is ``.gz``, - the file is first decompressed. + File or filename to read. If the filename extension is ``.gz`` or + ``.bz2``, the file is first decompressed. dtype : data-type Data type of the resulting array. If this is a record data-type, the resulting array will be 1-dimensional, and each row will be @@ -320,6 +320,9 @@ if fname.endswith('.gz'): import gzip fh = gzip.open(fname) + elif fname.endswith('.bz2'): + import bz2 + fh = bz2.BZ2File(fname) else: fh = file(fname) elif hasattr(fname, 'seek'):
Index: numpy/lib/io.py =================================================================== --- numpy/lib/io.py (revision 6085) +++ numpy/lib/io.py (working copy) @@ -333,7 +333,7 @@ fh = gzip.open(fname) else: fh = file(fname) - elif hasattr(fname, 'seek'): + elif hasattr(fname, 'readline'): fh = fname else: raise ValueError('fname must be a string or file handle')
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion