Skip to content

Commit

Permalink
ENH: Add support for float hex format to loadtxt.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
claumann authored and charris committed Feb 13, 2015
1 parent 35d01b2 commit 4aef6a8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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_):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4aef6a8

Please sign in to comment.