[英]solving the comparison of unicode input string in the file with unicode data
string1=" म नेपाली हुँ"
string1=string1.split()
string1[0]
'\xe0\xa4\xae'
with codecs.open('nepaliwords.txt','r','utf-8') as f:
for line in f:
if string1[0] in line:
print "matched string found in file"
追溯(最近一次通话最后一次):文件“”,第3行,UnicodeDecodeError:“ ascii”编解码器无法解码位置0的字节0xe0:序数不在范围内(128)
在文本文件中,我有大量的尼泊尔unicode。
在比较两个unicode字符串时,我在做错什么吗?
如何打印匹配的unicode字符串?
您的string1
是一个字节字符串 ,编码为UTF-8。 它不是 Unicode字符串。 但是您使用了codecs.open()
来让Python将文件内容解码为unicode
。 然后尝试将您的字节字符串与包含测试一起使用,会导致Python将字节字符串隐式解码为unicode
以匹配类型。 由于隐式解码使用ASCII,因此失败。
首先将string1
解码为unicode
:
string1 = " म नेपाली हुँ"
string1 = string1.decode('utf8').split()[0]
或改用Unicode字符串文字:
string1 = u" म नेपाली हुँ"
string1 = string1.split()[0]
在开始时注意u
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.