summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/npyio.py7
-rw-r--r--numpy/lib/tests/test_io.py22
2 files changed, 18 insertions, 11 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index a203cc67b..306b9ce07 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -612,8 +612,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
A dictionary mapping column number to a function that will convert
that column to a float. E.g., if column 0 is a date string:
``converters = {0: datestr2num}``. Converters can also be used to
- provide a default value for missing data:
- ``converters = {3: lambda s: float(s or 0)}``. Default: None.
+ provide a default value for missing data (but see also `genfromtxt`):
+ ``converters = {3: lambda s: float(s.strip() or 0)}``. Default: None.
skiprows : int, optional
Skip the first `skiprows` lines; default: 0.
usecols : sequence, optional
@@ -627,6 +627,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
ndmin : int, optional
The returned array must have at least `ndmin` dimensions.
Legal values: 0 (default), 1 or 2.
+ .. versionadded:: 1.6.0
Returns
-------
@@ -734,7 +735,7 @@ 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:
return line.split(delimiter)
else:
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 53068838a..4894f8939 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -417,10 +417,8 @@ class TestLoadTxt(TestCase):
"Test using an explicit dtype with an object"
from datetime import date
import time
- data = """
- 1; 2001-01-01
- 2; 2002-01-31
- """
+ data = asbytes(""" 1; 2001-01-01
+ 2; 2002-01-31 """)
ndtype = [('idx', int), ('code', np.object)]
func = lambda s: strptime(s.strip(), "%Y-%m-%d")
converters = {1: func}
@@ -457,6 +455,16 @@ class TestLoadTxt(TestCase):
finally:
os.unlink(name)
+ def test_empty_field_after_tab(self):
+ c = StringIO()
+ c.write(asbytes('1 \t2 \t3\tstart \n4\t5\t6\t \n7\t8\t9.5\t'))
+ c.seek(0)
+ dt = { 'names': ('x', 'y', 'z', 'comment'),
+ 'formats': ('<i4', '<i4', '<f4', '|S8')}
+ x = np.loadtxt(c, dtype=dt, delimiter='\t')
+ a = np.array([asbytes('start '), asbytes(' '), asbytes('')])
+ assert_array_equal(x['comment'], a)
+
def test_structure_unpack(self):
txt = StringIO(asbytes("M 21 72\nF 35 58"))
dt = { 'names': ('a', 'b', 'c'), 'formats': ('|S1', '<i4', '<f4')}
@@ -802,10 +810,8 @@ M 33 21.99
"Test using an explicit dtype with an object"
from datetime import date
import time
- data = asbytes("""
- 1; 2001-01-01
- 2; 2002-01-31
- """)
+ data = asbytes(""" 1; 2001-01-01
+ 2; 2002-01-31 """)
ndtype = [('idx', int), ('code', np.object)]
func = lambda s: strptime(s.strip(), "%Y-%m-%d")
converters = {1: func}