summaryrefslogtreecommitdiff
path: root/numpy/lib/npyio.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r--numpy/lib/npyio.py43
1 files changed, 38 insertions, 5 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 9d539d6ac..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
@@ -1204,7 +1215,8 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
usecols=None, names=None,
excludelist=None, deletechars=None, replace_space='_',
autostrip=False, case_sensitive=True, defaultfmt="f%i",
- unpack=None, usemask=False, loose=True, invalid_raise=True):
+ unpack=None, usemask=False, loose=True, invalid_raise=True,
+ max_rows=None):
"""
Load data from a text file, with missing values handled as specified.
@@ -1285,6 +1297,12 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
If True, an exception is raised if an inconsistency is detected in the
number of columns.
If False, a warning is emitted and the offending lines are skipped.
+ max_rows : int, optional
+ The maximum number of rows to read. Must not be used with skip_footer
+ at the same time. If given, the value must be at least 1. Default is
+ to read the entire file.
+
+ .. versionadded:: 1.10.0
Returns
-------
@@ -1353,6 +1371,14 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')])
"""
+ if max_rows is not None:
+ if skip_footer:
+ raise ValueError(
+ "The keywords 'skip_footer' and 'max_rows' can not be "
+ "specified at the same time.")
+ if max_rows < 1:
+ raise ValueError("'max_rows' must be at least 1.")
+
# Py3 data conversions to bytes, for convenience
if comments is not None:
comments = asbytes(comments)
@@ -1451,7 +1477,11 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
names = validate_names(names)
# Get the dtype
if dtype is not None:
- dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names)
+ dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names,
+ excludelist=excludelist,
+ deletechars=deletechars,
+ case_sensitive=case_sensitive,
+ replace_space=replace_space)
# Make sure the names is a list (for 2.5)
if names is not None:
names = list(names)
@@ -1647,8 +1677,8 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
# Skip an empty line
if nbvalues == 0:
continue
- # Select only the columns we need
if usecols:
+ # Select only the columns we need
try:
values = [values[_] for _ in usecols]
except IndexError:
@@ -1661,7 +1691,10 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
append_to_rows(tuple(values))
if usemask:
append_to_masks(tuple([v.strip() in m
- for (v, m) in zip(values, missing_values)]))
+ for (v, m) in zip(values,
+ missing_values)]))
+ if len(rows) == max_rows:
+ break
if own_fhd:
fhd.close()