summaryrefslogtreecommitdiff
path: root/numpy/lib/npyio.py
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2021-08-02 15:16:12 +0300
committerRoss Barnowski <rossbar@berkeley.edu>2021-08-02 15:16:12 +0300
commit3d6a75df56bbe7e45e4f25e5f7c66337367e406d (patch)
tree4dd96f3bbae1c5b4d776fd65f9d7cd05bbe5139c /numpy/lib/npyio.py
parent2c1a34daa024398cdf9c6e1fbeff52b4eb280551 (diff)
downloadnumpy-3d6a75df56bbe7e45e4f25e5f7c66337367e406d.tar.gz
DOC/MAINT: Update wording re: fname inputs.
Both loadtxt and genfromtxt can read from lists and generators of strings. The genfromtxt docstring captures this, but loadtxt was missing this info. Minor updates to docstrings and exceptions to: - make the wording more accurate. - homogenize the docs/exception messages for loadtxt/genfromtxt. - Make a ValueError in loadtxt a TypeError instead, which is more accurate and aligns with genfromtxt.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r--numpy/lib/npyio.py23
1 files changed, 13 insertions, 10 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index d12482cb7..9581cc1d9 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -783,10 +783,11 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
Parameters
----------
- fname : file, str, or pathlib.Path
- File, filename, or generator to read. If the filename extension is
- ``.gz`` or ``.bz2``, the file is first decompressed. Note that
- generators should return byte strings.
+ fname : file, str, pathlib.Path, list of str, generator
+ File, filename, list, or generator to read. If the filename
+ extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note
+ that generators must return bytes or strings. The strings
+ in a list or produced by a generator are treated as lines.
dtype : data-type, optional
Data-type of the resulting array; default: float. If this is a
structured data-type, the resulting array will be 1-dimensional, and
@@ -1075,8 +1076,9 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
fh = iter(fname)
fencoding = getattr(fname, 'encoding', 'latin1')
except TypeError as e:
- raise ValueError(
- 'fname must be a string, file handle, or generator'
+ raise TypeError(
+ f"fname must be a string, filehandle, list of strings, "
+ f"or generator. Got {type(fname)} instead."
) from e
# input may be a python2 io stream
@@ -1581,8 +1583,8 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
----------
fname : file, str, pathlib.Path, list of str, generator
File, filename, list, or generator to read. If the filename
- extension is `.gz` or `.bz2`, the file is first decompressed. Note
- that generators must return byte strings. The strings
+ extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note
+ that generators must return bytes or strings. The strings
in a list or produced by a generator are treated as lines.
dtype : dtype, optional
Data type of the resulting array.
@@ -1801,8 +1803,9 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
fhd = iter(fid)
except TypeError as e:
raise TypeError(
- "fname must be a string, filehandle, list of strings, "
- "or generator. Got %s instead." % type(fname)) from e
+ f"fname must be a string, filehandle, list of strings, "
+ f"or generator. Got {type(fname)} instead."
+ ) from e
with fid_ctx:
split_line = LineSplitter(delimiter=delimiter, comments=comments,