我有遍历文件的python代码。(invalid continuation byte)
读取文件时出现UTF-8错误。我只希望我的程序忽略它。
我尝试过使用try,但里面的代码除外,但是由于错误处于for循环的情况下,因此无法正常工作。我也尝试过尝试,但在循环周围除外,但是当它捕获到错误时,它不会再次开始循环。
with open(input_file_path, "r") as input_file:
for line in input_file:
# code irrelevant to question
发生什么是在以下位置出现此错误for line in input_file
:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 5: invalid continuation byte`
我希望它跳过这一行并移至下一行。本质上,尝试抓住我的for循环的条件。
这样行吗?(已编辑为找到的解决方案OP)
with open(input_file_path, "r", encoding="utf8", errors="surrogateescape") as input_file:
for line in input_file:
try:
yourcode
except:
continue
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句