diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-03-11 10:07:53 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-03-11 11:21:06 -0600 |
commit | f1cfc4f00cb61a363976ea0e38c80500ab91d172 (patch) | |
tree | 0d42dd0b9f50a75ea78e10e89b7c08054d295e73 /numpy/lib/npyio.py | |
parent | a7bea17c679804a4bbde0676179c7c7b53b8ae00 (diff) | |
download | numpy-f1cfc4f00cb61a363976ea0e38c80500ab91d172.tar.gz |
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.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 9 |
1 files changed, 7 insertions, 2 deletions
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: |