我有一个非常基本的python问题。在
我正试图编写一个脚本来消除一些.csv文件中的一堆空白行,我编写的脚本可以处理大约90%的文件,但是有一些脚本会给我带来以下错误:
Traceback (most recent call last):
File "/Users/stephensmith/Documents/Permits/deleterows.py", line 17, in <module>
deleteRow(file, "output/" + file)
File "/Users/stephensmith/Documents/Permits/deleterows.py", line 8, in deleteRow
for row in csv.reader(input):
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_8_sig.py", line 69, in _buffer_decode
return codecs.utf_8_decode(input, errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 6540: invalid start byte
我的代码是:
^{pr2}$我尝试在open()语句中添加encoding='utf-8'、='ascii'和='latin1',但没有成功。:-(知道我做错了什么吗?如果有帮助的话,.csv文件是用excelformac2011创建的。在
也许你可以尝试循环使用csv文件,这些文件会崩溃,比如:
看看是否有可疑人物出现。在
如果您能够通过这种方式识别可疑字符,比如弹出\0Xý1,则可以通过重写和替换该字符来清理文件:
^{pr2}$然后用清理后的文件再试一次。在
这是一个编码问题。输入csv文件并不像Python平台预期的那样编码为utf-8。问题是,如果不知道它的编码,也没有一个有问题的行的例子,我真的猜不到编码。在
encoding='utf8'
和encoding='ascii'
都断开是正常的,因为有问题的字符是0xa2,它不在ascii范围内(<;=0x7f)不是有效的utf-8字符。但奇怪的是,encoding='latin1'
在同一个地方给出了相同的错误,因为0xa2在拉丁语中是¢
。在IMHO,根据this other SO post,您可以尝试
encoding='windows-1252'
,如果您的平台支持它。在如果仍然不起作用,您应该尝试识别
latin1
的有问题的行:
^{pr2}$special_opener
应该输出如下内容:(这行是有效的拉丁语,我是用
special_opener(file, 'utf8')
得到的)那你就可以在这里贴出冒犯的字眼了
相关问题 更多 >
编程相关推荐