[NumPy-Tickets] [NumPy] #1616: numpy.loadtxt uses both file-like and iterator interfaces instead of just one
NumPy Trac
numpy-tickets@scipy....
Sat Mar 26 12:42:07 CDT 2011
#1616: numpy.loadtxt uses both file-like and iterator interfaces instead of just
one
-------------------------+--------------------------------------------------
Reporter: zachrahan | Owner: somebody
Type: enhancement | Status: needs_review
Priority: normal | Milestone: 1.5.1
Component: Other | Version: devel
Keywords: |
-------------------------+--------------------------------------------------
Changes (by derek):
* status: new => needs_review
Comment:
Duh, WikiFormatting! Meant of course
{{{
BytesIO.__next__()
}}}
That makes it
{{{
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 9fbacaa..f236006 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -679,7 +679,10 @@ def loadtxt(fname, dtype=float, comments='#',
delimiter=None,
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):
@@ -737,14 +740,18 @@ def loadtxt(fname, dtype=float, comments='#',
delimiter=None,
# Skip the first `skiprows` lines
for i in xrange(skiprows):
- fh.readline()
+ try:
+ next(fh)
+ 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 = next(fh)
+ except StopIteration: # EOF reached
raise IOError('End-of-file reached before encountering
data.')
first_vals = split_line(first_line)
N = len(usecols or first_vals)
}}}
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1616#comment:2>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list