我有一个很基本的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
下面是我的代码:
import csv
import os
def deleteRow(in_fnam, out_fnam):
input = open(in_fnam, 'r')
output = open(out_fnam, 'w')
writer = csv.writer(output)
for row in csv.reader(input):
if any(row):
writer.writerow(row)
input.close()
output.close()
for file in os.listdir("/Users/stephensmith/Documents/Permits/"):
print(file)
if file.endswith(".csv"):
deleteRow(file, "output/" + file)
我试过在两个open()语句中添加encoding ='utf-8'、='ascii'和='latin 1',但没有成功。:-(知道我做错了什么吗?.csv文件是用Excel for Mac 2011创建的,如果有帮助的话。
3条答案
按热度按时间m528fe3b1#
也许您可以尝试使用类似以下内容循环遍历崩溃的csv文件:
看看有没有可疑人物出现。
如果你能用这种方法识别出可疑字符,比如 \0Xý1 弹出,你可以重写并替换这个字符来清理文件:
然后用清理过的文件再试一次。
e37o9pze2#
这是一个编码问题,输入的csv文件不是Python平台所期望的那种UTF-8编码,问题是,如果不知道它的编码,也没有一个错误行的例子,我真的猜不出它的编码。
encoding='utf8'
和encoding='ascii'
都被打断是正常的,因为有问题的字符是0xa 2,它不在ascii范围内(〈= 0x 7 f),不是一个有效的utf-8字符。但很奇怪的是,encoding='latin1'
在同一个地方给出了同样的错误,因为0xa 2在latin 1中是¢
。IMHO,根据this other SO post,如果您的平台支持
encoding='windows-1252'
,您可以尝试它。如果它仍然不起作用,您应该尝试找出
latin1
的违规行:special_opener
应输出如下内容:(this行是有效的latin 1,我用
special_opener(file, 'utf8')
得到的)然后您就可以在此处发布冒犯行
toiithl63#
我也遇到过类似的问题。在我的例子中,csv不是很大,在LibreOffice Calc中打开它并保存回来修复了它。