简体   繁体   中英

In a python for loop, how do you handle an iterator that can throw exceptions?

I am using an iterator, namely the standard library CSV reader, that can throw exceptions. Here's a minimal working code sample:

import csv
import sys

csv.field_size_limit(10_000_000)

reader = csv.reader(sys.stdin)
for row in reader:
    pass

And here's what happens when I run it:

[joel@vm all_recent]$ python dummy_csv.py < mycsv.csv
Traceback (most recent call last):
  File "/path/dummy_csv.py", line 7, in <module>
    for row in reader:
  File "/usr/lib64/python3.10/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 264: invalid start byte

What I'd like to be able to do is to log the exception, and continue iterating. What's the pythonic way to do this?

You can extract elements from the iterator manually instead of the for loop doing it for you.

reader = csv.reader(sys.stdin)
it = iter(reader)

while True:
    try:
        next(it)
    except StopIteration:
        break
    except Exception:
        # do something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM