我想從字典中搜索其單詞是否在第二個txt文件中。 我有下面的代碼的問題:Python:帶字典,在另一個文本文件中搜索字符串並打印整行
print 'Searching for known strings...\n'
with open('something.txt') as f:
haystack = f.read()
with open('d:\\Users\\something\\Desktop\\something\\dictionary\\entirelist.txt') as f:
for needle in (line.strip() for line in f):
if needle in haystack:
print line
的開放語句從我都沒有,我把他們從: Python: search for strings listed in one file from another text file? 我想打印行,所以我寫了行,而不是針。問題出現了:它說「線路未定義」。
我的最終目標是查看字典中的任何單詞是否在「something.txt」中,如果是,則打印單詞所在的行。 對不起,不好的英語或不好的問候,希望你能幫助我! Thx爲你的理解:)
你可以給我們一個例子(剝離下來,比如說3行)什麼'something.txt'和'wholelist.txt'看起來像什麼,你想要什麼輸出? – abarnert 2014-10-30 00:56:45