在python中打开带有重音符号的文本文件
问题描述:
我尝试使用Python 2.7打开法语文本文件.我使用了命令
I try to open a text file in French with Python 2.7. I used the command
f=open('textfr','r')
但是当我使用
f.read()
我失去了重音符号:我得到的是u"J'\xc3\xa9tais \xc3\xa0
巴黎,而不是J'étais à
巴黎,等等.
I lose accented characters: I get u"J'\xc3\xa9tais \xc3\xa0
Paris instead of J'étais à
Paris, etc..
在linux终端中,我会做
when in linux terminal, I do
file -i textfr
我知道
charset=utf-8
所以我不明白...
答
您需要指定字符集.
f = io.open('textfr', 'r', encoding='utf-8')