From f1cfc4f00cb61a363976ea0e38c80500ab91d172 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Wed, 11 Mar 2015 10:07:53 -0600 Subject: BUG: loadtxt fails with complex data in Python 3. The problem is that the Python complex type constructor only accepts a pair of numbers or a string, unlike other numeric types it does not work with byte strings. The numpy error is subtle, as loadtxt opens the file in the default text mode, but then converts the input lines to byte strings when they are split into separate values. The fix here is to convert the values back to strings in the complex converter. Closes #5655. --- numpy/lib/npyio.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'numpy/lib/npyio.py') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index bf703bb76..5ebeae6c3 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -690,7 +690,7 @@ def _getconv(dtype): elif issubclass(typ, np.floating): return floatconv elif issubclass(typ, np.complex): - return complex + return lambda x: complex(asstr(x)) elif issubclass(typ, np.bytes_): return bytes else: @@ -863,7 +863,12 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, return tuple(ret) def split_line(line): - """Chop off comments, strip, and split at delimiter.""" + """Chop off comments, strip, and split at delimiter. + + Note that although the file is opened as text, this function + returns bytes. + + """ if comments is None: line = asbytes(line).strip(asbytes('\r\n')) else: -- cgit v1.2.1