我尝试使用 pandas 读取文本文件格式的数据集。但是,某些字符编码不正确。我有 ???用于撇号。
我应该怎么做才能正确编码我的文件?我试过了
encoding = "utf8"但我得到了UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 2044: unexpected end of data。encoding = "latin1"但这给了我很多 ???encoding = "ISO-8859-1"or "ISO-8859-2"但这也让我觉得没有编码...
当我在 sublime 中打开我的数据时,我得到了这个字符 '.
更新:但是当我使用 loc 访问条目时,我得到了类似\u0102\u02d8\xe2\x82\u0179\xc2\u015,\u0102\u02d8\xe2\x82\u0179\xe2\x84\u02d8
请您参考如下方法:
您可以 determine the encoding与 chardet :
$ pip install chardet
>>> import urllib
>>> rawdata = urllib.urlopen('http://yahoo.co.jp/').read()
>>> import chardet
>>> chardet.detect(rawdata)
{'encoding': 'EUC-JP', 'confidence': 0.99}
基本用法还建议您如何使用它来推断大文件的编码,例如文件太大而无法读入内存 - 它会读取文件,直到它对编码有足够的信心。
根据 this answer你应该试试encoding="ISO-8859-2":
My guess is that your input is encoded as ISO-8859-2 which contains Ă as
0xC3.
注意:Sublime 可能无法正确推断编码,因此您必须对它的输出持保留态度,最好与您的供应商(无论您从哪里获取文件)核实实际编码是什么是……
