在python中打开带有重音符号的文本文件 [英] Open a text file with accents in python

查看:117
本文介绍了在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')

这篇关于在python中打开带有重音符号的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆