summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorAntony Lee <anntzer.lee@gmail.com>2021-08-05 18:01:04 +0200
committerGitHub <noreply@github.com>2021-08-05 19:01:04 +0300
commitcc7f1504aaf7b02de09ba87ee91dfe4446d90031 (patch)
tree5426d2531bfdfe03c500f10654e838665e3cd451 /numpy
parentd97e31e891e868457d710d53b8ddadb0d473ec8a (diff)
downloadnumpy-cc7f1504aaf7b02de09ba87ee91dfe4446d90031.tar.gz
MAINT: In loadtxt, refactor detection of the number of columns. (#19616)
`for... else...` seems more idiomatic than manual iteration and catching StopIteration. Also rename the slightly cryptic `N` to a clearer `ncols`.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/npyio.py27
1 files changed, 12 insertions, 15 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 3b6a1c563..7c73d9655 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1007,7 +1007,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
continue
if usecols:
vals = [vals[j] for j in usecols]
- if len(vals) != N:
+ if len(vals) != ncols:
line_num = i + skiprows + 1
raise ValueError("Wrong number of columns at line %d"
% line_num)
@@ -1107,22 +1107,19 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
for i in range(skiprows):
next(fh)
- # Read until we find a line with some values, and use
- # it to estimate the number of columns, N.
- first_vals = None
- try:
- while not first_vals:
- first_line = next(fh)
- first_vals = split_line(first_line)
- except StopIteration:
- # End of lines reached
+ # Read until we find a line with some values, and use it to determine
+ # the need for decoding and estimate the number of columns.
+ for first_line in fh:
+ ncols = len(usecols or split_line(first_line))
+ if ncols:
+ break
+ else: # End of lines reached
first_line = ''
- first_vals = []
+ ncols = len(usecols or [])
warnings.warn('loadtxt: Empty input file: "%s"' % fname,
stacklevel=2)
- N = len(usecols or first_vals)
- # Now that we know N, create the default converters list, and
+ # Now that we know ncols, create the default converters list, and
# set packing, if necessary.
if len(dtype_types) > 1:
# We're dealing with a structured array, each field of
@@ -1131,8 +1128,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
else:
# All fields have the same dtype; use specialized packers which are
# much faster than those using _loadtxt_pack_items.
- converters = [defconv for i in range(N)]
- if N == 1:
+ converters = [defconv for i in range(ncols)]
+ if ncols == 1:
packer = itemgetter(0)
else:
def packer(row): return row