我有一个逗号分隔
.txt
带有法语字符的文件,例如
Vétérinaire
Désinfectant
。
import pandas as pd
df = pd.read_csv('somefile.txt', sep=',', header=None, encoding='utf-8')
[Decode error - output not utf-8]
我读过很多问答帖子(包括
this
)尝试了很多不同的编码,比如
latin1
'和'
utf-16
'他们没用。但是,我尝试在不同的Windows 10计算机上运行完全相同的脚本,使用类似的Python设置(所有的Python 3.6),它在另一台计算机上运行得非常好。
编辑:我试过
this
encoding='cp1252'
有助于
.txt文件
我想导入的文件,但有几个
.txt文件
File "C:\Program_Files_Extra\Anaconda3\lib\encodings\cp1252.py", line 15, in decode
return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 25: character maps to <undefined>
编辑:
试图从chardet中识别编码
import chardet
import pandas as pd
test_txt = 'somefile.txt'
rawdata = open(test_txt, 'rb').read()
result = chardet.detect(rawdata)
charenc = result['encoding']
print (charenc)
df = pd.read_csv(test_txt, sep=',', header=None, encoding=charenc)
print (df.head())
utf-8
[Decode error - output not utf-8]