【发布时间】:2014-04-03 17:10:44
【问题描述】:
我正在尝试弄清楚如何根据单词列表 (ylist) 检查文件列表 (wfiles) 的内容,然后打印文件名并确认是否找到了 ylist 中的单词.
这是 wfiles:
wfiles = ['a.txt', 'b.txt', 'c.txt']
这是a.txt的内容:
hello jim this is tom
the serial code: x029-1029-2031
the password is bananaappleorange. grapes
cheer for the grapes
regards, tom
这是b.txt的内容:
this is a test not a joke, though I'm kidding.
lambda is firthy 23 too.
这是c.txt的内容:
is
not
here
xyz
069
@heytheremate. this is your friend. how are you?
为了解决这个问题,我有:
something = 'myfolder'
ylist = ['grapes', 'name']
dmd = os.listdir(something)
wfiles = []
for i in dmd:
if ".txt" in i:
wfiles.append(item)
for w in wfiles:
with open(something + '/' + w) as ofiles:
for xlist in ofiles:
if any(word in xlist for word in ylist):
print w, 'FOUND'
break;
else:
print w, 'NOTFOUND'
break;
值得注意的是,在 a.txt 的实例中,'grapes' 和 'name' 都存在(来自 ylist)并且应该打印 'FOUND',但是在 b.txt 和 c.text 的实例中,它们没有包括其中另一个词,当他们的案例中应该打印“NOTFOUND”时,也打印了“FOUND”。
这是我运行代码后收到的:
a.txt FOUND
b.txt FOUND
c.txt FOUND
我在这里做错了什么?
【问题讨论】:
-
您是否应该在其中某处添加
file.read()?
标签: python python-2.7