从文本文件读取非ASCII字符 [英] Reading non-ASCII characters from a text file
本文介绍了从文本文件读取非ASCII字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是python 2.7。我试过很多事情,如编解码器,但没有工作。如何解决此问题。
I'm using python 2.7. I've tried many things like codecs but didn't work. How can I fix this.
myfile.txt
myfile.txt
wörd
我的代码
f = open('myfile.txt','r')
for line in f:
print line
f.close()
输出
s\xc3\xb6zc\xc3\xbck
输出在eclipse和命令窗口上是相同的。我使用Win7。当我没有从文件中读取任何字符没有问题。
Output is same on eclipse and command window. I'm using Win7. There is no problem with any characters when I don't read from a file.
推荐答案
- 首先 - 检测编码
from chardet import detect
encoding = lambda x: detect(x)['encoding']
print encoding(line)
- 然后 - 将其转换为unicode或默认编码str:
n_line=unicode(line,encoding(line),errors='ignore')
print n_line
print n_line.encode('utf8')
这篇关于从文本文件读取非ASCII字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文