String 如何在python中搜索列表中的特定unicode标记

String 如何在python中搜索列表中的特定unicode标记,string,python-2.7,list,unicode,String,Python 2.7,List,Unicode,我想从文件中搜索一个特定的字符串,其代码如下 f1= codecs.open('brokenhindi.txt', encoding='utf-8') for tokens in f1: if u"राज्य" in tokens: print 'done_3' 但它没有搜索字符串(राज्य), 如果我替换राज्य 然后用英文标记搜索。我在代码中找不到错误。您的代码运行正常。 看起来您的脚本或文本文件是ascii编码的,而不是utf-8。 尝试将其保存为utf-8编

我想从文件中搜索一个特定的字符串,其代码如下

f1= codecs.open('brokenhindi.txt', encoding='utf-8')
for tokens in f1:
    if u"राज्य" in tokens:
        print 'done_3'
但它没有搜索字符串(राज्य), 如果我替换राज्य 然后用英文标记搜索。我在代码中找不到错误。

您的代码运行正常。 看起来您的脚本或文本文件是ascii编码的,而不是utf-8。 尝试将其保存为utf-8编码