簡體   English   中英

utf-8在列表中搜索單詞

[英]utf-8 search for word in list

我有一個從utf-8文件生成的查找列表

with open('stop_word_Tiba.txt') as f:
    newStopWords= list(itertools.chain( line.split() for line in f)) #save the file as list of lines
newStopWords1d=list(itertools.chain(*newStopWords)) # convert 2d list to 1d list

當我打開文件時,我看到其中有單詞“الو”。 因此它在列表中,但列表現在看起來像['\\ xd8 \\ xa7 \\ xd9 \\ x84 \\ xd9 \\ x88','\\ xd8 \\ xa3 \\ xd9 \\ x84 \\ xd9 \\ x88','\\ xd8 \\ xa7 \\ xd9 \\ x88 \\ xd9 \\ x83 \\ xd9 \\ x8a','\\ xd8 \\ xa7 \\ xd9 \\ x84','\\ xd8 \\ xa7 \\ xd9 \\ x87','\\ xd8 \\ xa3 \\ xd9 \\ x87','\\ xd9 \\ x87 \\ xd9 \\ x84 \\ xd9 \\ x88','\\ xd8 \\ xa3 \\ xd9 \\ x88 \\ xd9 \\ x83 \\ xd9 \\ x8a','\\ xd9 \\ x88']

然后我想搜索newStopWords1d中是否有特定單詞,單詞'الو'是'\\ xd8 \\ xa7 \\ xd9 \\ x84 \\ xd9 \\ x88'

word='الو'
for w in newStopWords1d:
    if word == w.encode("utf-8"):
        print 'found'

找不到單詞,我試過了

    if word in newStopWords1d:
        print 'found'

但同樣沒有看到這個詞。 似乎是編碼問題,但我無法解決。 你能幫我么。

值得一提的是您使用的是Python 2.7。

word='الو'
for w in newStopWords1d:
    if word == w.decode("utf-8"):
        print 'found'

更好的解決方案是使用io的open函數

import io

with io.open('stop_word_Tiba.txt', encoding="utf-8") as f:
    ...

codecs模塊

import codecs

with codecs.open('stop_word_Tiba.txt', encoding="utf-8") as f:
    ...

因為Python 2.7中的內置open函數不支持指定編碼。

通過將打開文件語句編輯為

with codecs.open("stop_word_Tiba.txt", "r", "utf-8") as f:
    newStopWords= list(itertools.chain( line.split() for line in f)) #save the file as list of lines
newStopWords1d=list(itertools.chain(*newStopWords))
    for w in newStopWords1d:
            if word.encode("utf-8") == w.encode("utf-8") :  
                      return 'found'

謝謝你..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM