[Numpy-discussion] numpy.loadtxt requires seek()?

Ryan May rmay31@gmail....
Thu Nov 20 13:22:38 CST 2008


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'm trying to use loadtxt() directly with the file-like object returned
by urllib2.urlopen().  If I change the check for 'seek' to one for
'readline', using the urlopen object works with a hitch.

As far as I can tell, all the "filehandle" object needs to meet is:

1) Have a readline() method so that loadtxt can skip the first N lines
and read the first line of data

2) Be compatible with itertools.chain() (should be any iterable)

At a minimum, I'd ask to change the check for 'seek' to one for 'readline'.

On a bit deeper thought, it would seem that loadtxt would work with any
iterable that returns individual lines.  I'd like then to change the
calls to readline() to just getting the next object from the iterable
(iter.next() ?) and change the check for a file-like object to just a
check for an iterable.  In fact, we could use the iter() builtin to
convert whatever got passed.  That would give automatically a next()
method and would raise a TypeError if it's incompatible.

Thoughts?  I'm willing to write up the patch for either

Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

More information about the Numpy-discussion mailing list