我制作的脚本和预处理大的CSV导入到数据库:如何在python3中跳过枚举编码异常?
with open(sys.argv[1], encoding='utf-16') as _f:
for i, line in enumerate(_f):
try:
.... some stuff with line ...
except Exception as e:
...
但在某些时候它给了我异常的enumerate
:
...
File "/Users/elajah/PycharmProjects/untitled1/importer.py", line 94, in main
for i, line in enumerate(_f):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/utf_16.py", line 69, in _buffer_decode
return self.decoder(input, self.errors, final)
UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x00 in position 0: truncated data
...
如何跳过虚线文件不中断脚本流 ?
谢谢我试过这个,但在这种情况下,事情是在'enumerate'函数调用过程中发生的,唯一可行的解决方案是我看不到使用它... –
你可以把整个'for ...在“try:”块内的enumarate()块中。但是,在你的'importer.py'脚本中使用'try:'可能会更好,在那里发生初始错误,并尝试修复那里的错误。 – C14L