summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/lib/npyio.py8
-rw-r--r--numpy/lib/tests/test_io.py8
2 files changed, 14 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index bedd0e941..a40de4fea 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -729,7 +729,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
"""
# Type conversions for Py3 convenience
- comments = asbytes(comments)
+ if comments is not None:
+ comments = asbytes(comments)
user_converters = converters
if delimiter is not None:
delimiter = asbytes(delimiter)
@@ -802,7 +803,10 @@ 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(asbytes('\r\n'))
+ if comments is None:
+ line = asbytes(line).strip(asbytes('\r\n'))
+ else:
+ 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 68b2018cd..81bddfadd 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -786,6 +786,14 @@ class TestLoadTxt(TestCase):
# Check for exception and that exception contains line number
assert_raises_regex(ValueError, "3", np.loadtxt, c)
+ def test_none_as_string(self):
+ # gh-5155, None should work as string when format demands it
+ c = TextIO()
+ c.write('100,foo,200\n300,None,400')
+ c.seek(0)
+ dt = np.dtype([('x', int), ('a', 'S10'), ('y', int)])
+ data = np.loadtxt(c, delimiter=',', dtype=dt, comments=None)
+
class Testfromregex(TestCase):
# np.fromregex expects files opened in binary mode.