[NumPy-Tickets] [NumPy] #1071: loadtxt fails if the last column contains empty value
NumPy Trac
numpy-tickets@scipy....
Thu Mar 31 17:19:04 CDT 2011
#1071: loadtxt fails if the last column contains empty value
---------------------------------+------------------------------------------
Reporter: Electrion | Owner: somebody
Type: defect | Status: needs_review
Priority: normal | Milestone: 1.6.0
Component: numpy.lib | Version: devel
Keywords: loadtxt ascii strip |
---------------------------------+------------------------------------------
Comment(by derek):
Replying to [comment:7 bsouthey]:
> Replying to [comment:5 derek]:
> If I just use 'line = line.split(comments)[0].strip('\r\n')' in version
2.0.0.dev-7134a93 then this example now works for me.
Not converting the string with {{{asbytes('\r\n')}}} raises an error for
me with Python3, and without the blank it fails this test:
{{{
ERROR: Test using an explicit dtype with an object
----------------------------------------------------------------------
Traceback (most recent call last):
File "/sw/lib/python2.6/site-packages/numpy/lib/tests/test_io.py", line
422, in test_dtype_with_object
converters=converters)
File "/sw/lib/python2.6/site-packages/numpy/lib/npyio.py", line 784, in
loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/sw/lib/python2.6/site-packages/numpy/lib/npyio.py", line 569, in
<lambda>
return lambda x: int(float(x))
ValueError: empty string for float()
}}}
because the test passes two blank lines as input. It is not explicitly
documented that loadtxt silently skips blank lines, and it could raise
other issues because empty lines have been proposed to delimit different
data blocks in #1107.
But if we want to preserve this functionality, the better fix I now
realise is
{{{
@@ -724,8 +728,8 @@ def loadtxt(fname, dtype=float, comments='#',
delimiter=None,
def split_line(line):
"""Chop off comments, strip, and split at delimiter."""
- line = asbytes(line).split(comments)[0].strip()
+ line = asbytes(line).split(comments)[0].strip(asbytes('\r\n'))
+ if line.strip():
return line.split(delimiter)
else:
return []
}}}
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1071#comment:8>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list