【发布时间】:2014-10-30 00:46:52
【问题描述】:
我想从字典中搜索其中一个单词是否在第二个 txt 文件中。 我对以下代码有疑问:
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
with open 语句不是来自我,我是从:
Search for strings listed in one file from another text file?
我想打印这条线,所以我写的是线而不是针。问题来了:上面写着line is not defined。
我的最终目标是查看字典中是否有任何单词在“something.txt”中,如果是,则打印识别出该单词的行。
【问题讨论】:
-
您能给我们举个例子(简化为 3 行)
something.txt和entirelist.txt的样子,以及您想要的输出吗?
标签: python