I am reading a large (2Gb) file via csv.DictReader
(for what it matters). Somewhere deep in the file there is an issue with the coding and I get a UnicodeDecodeError
. Looking at the error message I see that the error is raised in the implicit __next__
in my for
loop.
Code stub would look at follows:
import csv
with open("myfile.csv", newline="") as f:
c = csv.DictReader(f)
for line in c: # here the error is happening
pass
I want to use a try-except
pattern to catch any reading error, logging a meaningful message and continue reading through the file.
How could I achieve that? I cannot use continue
outside the loop (ie in the except
block) so I guess I would need to rewrite the for
loop in order not to use the implicit form but an explicit one, but as I am fairly new to python, I do not know how to do that in the most pythonic way.
To simulate the error look at the following toy example:
class MyIterator:
def __init__(self, mclass):
self._mclass = mclass
self._index = 0
def __next__(self):
if self._index == 3:
# simulate an error in a file read
self._index += 1
raise Exception("simulated error"
elif self._index < 5:
self._index += 1
return self._index - 1
# End of Iteration
raise StopIteration
class MyClass:
def __iter__(self):
return MyIterator(self)
obj = MyClass()
try:
for result in obj:
print(result)
except Exception as e:
print("Exception covered")
## does not work for obvious reasons:
# continue
只是为了记录,而不是试图在next
上捕获错误,您可以将errors
参数传递给open
,这决定了如何处理编解码器错误。
Wrap your object in an iter and call next on it in a while loop
done = False
obj = iter(MyClass())
while not done:
try:
data = next(obj)
except StopIteration:
done = True
except Exception:
print("process error here and continue")
What python version are u using? Also why not using yield ( see enter link description here )
You can return instead of raise. If you know if type(variable) is Exception you cand process information.
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.