diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 2985f996aa83..df337efb67f9 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1136,9 +1136,6 @@ def convert_row(vals): for i, (lineno, words) in enumerate(lineno_words_iter): if usecols: words = usecols_getter(words) - elif len(words) != ncols: - raise ValueError( - f"Wrong number of columns at line {lineno}") try: X[i] = tuple(words) # Try implicit conversion of strs. continue # OK, done. @@ -1146,8 +1143,13 @@ def convert_row(vals): # Resize, and, for simplicity, use explicit converters too. X.resize(2 * len(X), refcheck=False) except ValueError: - # Fallback to explicit converters. - pass + # ValueError can be raised either by a length mismatch... + if len(words) != ncols: + raise ValueError( + f"Wrong number of columns at line {lineno}" + ) from None + # Or because the explicit (more lenient) converter (below) + # is needed. X[i] = convert_row(words) if i is None: X = None