>> Though, really, it's annoying that numpy.loadtxt needs both the >> readline function *and* the iterator protocol. If it just used >> iterators, you could do: >> >> def truncator(fh, delimiter='END'): >> for line in fh: >> if line.strip() == delimiter: >> break >> yield line >> >> numpy.loadtxt(truncator(c)) >> >> Maybe I'll try to work up a patch for this.
http://projects.scipy.org/numpy/ticket/1616 Zach > > > That seemed easy... worth applying? Won't break compatibility, because > the previous loadtxt required both fname.readline and fname.__iter__, > while this requires only the latter. > > > Index: numpy/lib/npyio.py > =================================================================== > --- numpy/lib/npyio.py (revision 8716) > +++ numpy/lib/npyio.py (working copy) > @@ -597,10 +597,11 @@ > fh = bz2.BZ2File(fname) > else: > fh = open(fname, 'U') > - elif hasattr(fname, 'readline'): > - fh = fname > else: > - raise ValueError('fname must be a string or file handle') > + try: > + fh = iter(fname) > + except: > + raise ValueError('fname must be a string or file handle') > X = [] > > def flatten_dtype(dt): > @@ -633,14 +634,18 @@ > > # Skip the first `skiprows` lines > for i in xrange(skiprows): > - fh.readline() > + try: > + fh.next() > + except StopIteration: > + raise IOError('End-of-file reached before > encountering data.') > > # Read until we find a line with some values, and use > # it to estimate the number of columns, N. > first_vals = None > while not first_vals: > - first_line = fh.readline() > - if not first_line: # EOF reached > + try: > + first_line = fh.next() > + except StopIteration: > raise IOError('End-of-file reached before > encountering data.') > first_vals = split_line(first_line) > N = len(usecols or first_vals) > > _______________________________________________ > NumPy-Discussion mailing list > [email protected] > http://mail.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
