我有一個巨大的utf8編碼csv文件,但一些編碼與主文件編碼不同的列。它看起來像:Python3 CSV閱讀器Unicode解碼錯誤
input.txt中在UTF-8編碼:
a,b,c
d,"e?",f
g,h,"kü"
相同input.txt中在Win-1252
a,b,c
d,"eü",f
g,h,"kü
代碼:
import csv
file = open("input.txt",encoding="...")
c = csv.reader(file, delimiter=';', quotechar='"')
for itm in c:
print(itm)
和standart python3 csv閱讀器屬tes在這樣的行上編碼錯誤。我不能忽略閱讀這一行,但我只需要總是好的編碼「someOther」列。
是否可以使用standart csv閱讀器在某些「字節模式」下以某種方式拆分CSV數據,然後將每個數組元素轉換爲普通的python unicode字符串,還是應該實現自己的csv閱讀器?
回溯:
Traceback (most recent call last):
File "C:\Development\t.py", line 7, in <module>
for itm in c:
File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 11: invalid start byte
我認爲'encoding =「...」'會導致問題。 –
它的僞代碼,我只想提到utf-8或ascii或某些特殊編碼都不起作用。 – Oleg
錯誤究竟是什麼?你可以發佈回溯?是CSV閱讀器中的錯誤,還是當您嘗試打印該行時? – mhawke