diff options
author | Chris Laumann <chris.laumann@gmail.com> | 2011-08-03 22:35:43 -0400 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-02-13 10:47:34 -0700 |
commit | 4aef6a89fda5015129e124099f3809fa4da894a7 (patch) | |
tree | f098073bd23121723254218a9d93097c1f5bc00b /numpy/lib/npyio.py | |
parent | 35d01b2a7cd38b2fda54b148402919aa1dd7e9c4 (diff) | |
download | numpy-4aef6a89fda5015129e124099f3809fa4da894a7.tar.gz |
ENH: Add support for float hex format to loadtxt.
Add _floatconv to npyio.py as a default floating point converter. This
uses float() as a type conversion with a fallback on (ValueError) to
float.fromhex().
Closes #2517.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 90ef83f4d..0632ba1f8 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -621,6 +621,13 @@ def _savez(file, args, kwds, compress): def _getconv(dtype): """ Find the correct dtype converter. Adapted from matplotlib """ + + def floatconv(x): + x.lower() + if b'0x' in x: + return float.fromhex(asstr(x)) + return float(x) + typ = dtype.type if issubclass(typ, np.bool_): return lambda x: bool(int(x)) @@ -631,7 +638,7 @@ def _getconv(dtype): if issubclass(typ, np.integer): return lambda x: int(float(x)) elif issubclass(typ, np.floating): - return float + return floatconv elif issubclass(typ, np.complex): return complex elif issubclass(typ, np.bytes_): @@ -706,6 +713,10 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, `genfromtxt` function provides more sophisticated handling of, e.g., lines with missing values. + .. versionadded:: 1.10.0 + The strings produced by the Python float.hex method can be used as + input for floats. + Examples -------- >>> from StringIO import StringIO # StringIO behaves like a file object |