This is a for
loop in Python:
for_stmt ::= "for" target_list "in" expression_list ":" suite
Normally, when yielding a value from the expression_list
raises an exception, the loop aborts. Is there an elegant way (short of rewriting the loop using while True
or something similar) to catch this exception and continue the loop?
Here is an example:
import csv
csv.field_size_limit(10)
reader = csv.reader(open('test.csv', 'r'))
for line in reader:
print(line)
with this file:
foo,bar,baz
xxx,veryverylong,yyy
abc,def,ghi
This aborts at the second line. I would like a way to skip or log the failing lines and continue.
question from:https://stackoverflow.com/questions/13653783/how-to-catch-an-exception-in-the-for-loop-iterator