Search code examples
pythonloopsexceptioniterator

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?


Solution

  • 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