Skip to content

Commit

Permalink
MAINT: In loadtxt, refactor detection of the number of columns. (nump…
Browse files Browse the repository at this point in the history
…y#19616)

`for... else...` seems more idiomatic than manual iteration and catching
StopIteration.
Also rename the slightly cryptic `N` to a clearer `ncols`.
  • Loading branch information
anntzer authored Aug 5, 2021
1 parent d97e31e commit cc7f150
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ def read_data(chunk_size):
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)
Expand Down Expand Up @@ -1107,22 +1107,19 @@ def read_data(chunk_size):
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
Expand All @@ -1131,8 +1128,8 @@ def read_data(chunk_size):
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
Expand Down

0 comments on commit cc7f150

Please sign in to comment.