法语字符变成问号;大 pandas [英] French Character Turn Into Question Marks; Pandas
问题描述
我有一个csv文件,其中包含法语字符/重音,包括:É,ê,è等,指的是一些法国城市和街道名称.我在Pandas的read_csv和to_csv函数上尝试了几种编码选项,包括:
I have a csv file which contains french characters/accents including: É, ê, è etc, referring to some french city and street names. I have tried several encoding options on the read_csv and to_csv functions in Pandas including:
df=pd.read_csv(FilePath, encoding='latin-1' )
还:
encoding='utf-8'
encoding='latin-1'
encoding='utf-8-sig'
encoding='iso-8859-1'
我也尝试过不指定任何编码.
I have also tried not specifying any encoding.
我正在使用Python 2.7和Pandas模块.我已经读过Python 3在编码方面做得更好,但这目前不是一个选择.
I am using Python 2.7 and the Pandas Module. I have read that Python 3 does better with encoding but that is not currently an option.
在excel或notepad ++中打开输出文件时,法语字符变成问号(?),现在由于试图解决该问题,当我读取原始文件或打开该文件时,它们以问号开始excel或notepadd ++中的原始文件.在他们出现正常的法语字符之前.
The french characters turn into questions marks (?) when the output file is opened in excel or notepad++, and now due to trying to fix that issue they begin as questions marks when I read in the original file or when I open that original file in excel or notepadd++. Before they showed up as normal french characters.
示例数据和代码:
City Address1_Particule Address1_Street Address1_StreetType
Montr? V Des BRISES DU FLEUVE ALL?
Montr? V Des BRISES DU FLEUVE ALL?
Montr? V Des BRISES DU FLEUVE ALL?
Montr? V Des BRISES DU FLEUVE ALL?
#create dataframe
df=pd.read_csv(FilePath, encoding='latin-1' )
for streetType in StreetTypeList:
for pretype in StreePreTypeList:
df[pretype]=''
# Change street type french from short to long form and into new column
df.loc[dfCAS[streetType]=='AV', [pretype]]='AVENUE'
df.loc[dfCAS[streetType]=='AVE', [pretype]]='AVENUE'
df.loc[dfCAS[streetType]=='BOUL', [pretype]]='BOULEVARD'
df.loc[dfCAS[streetType]=='CH', [pretype]]='CHEMIN'
df.to_csv(OutputPath, encoding='latin-1'
我希望创建一个输出csv文件,法语字符可以正确显示.
I hope to create an output csv file where french characters display properly.
谢谢您的帮助!
推荐答案
这应该有效
df = pd.read_excel(FilePath, encoding='latin1')
这篇关于法语字符变成问号;大 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!