根据Python中的单词列表检查文件列表的内容

时间:2014-04-03 08:03:16

标签: python python-2.7

我正在试图弄清楚如何根据单词列表(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的例子中,'grape'和'name'都存在(来自ylist)并且应该打印'FOUND',但是在b.txt和c.text的实例中,其中没有包括另一个单词,当'NOTFOUND'应该在他们的案例中打印时,也打印出'FOUND'。

这是我在运行代码后收到的内容:

a.txt FOUND
b.txt FOUND
c.txt FOUND

我在这里做错了什么?

3 个答案:

答案 0 :(得分:0)

这一行:

with open(w) as ofiles:

open(w)返回一个文件对象。我想你需要:

for xlist in ofiles.read().split():

获取文件中的单词。

这是您修改的代码 - 适用于我(对于您的三个文件):

>>> for w in wfiles:
...     with open(w) as ofiles:
...             if any(word in ofiles.read().split() for word in ylist):
...                     print w,'found'
... 
a.txt found

答案 1 :(得分:0)

wfiles = ['a.txt','b.txt','c.txt']
ylist = ['grapes', 'name']

for w in wfiles:
    with open(w) as ofiles:
        if any(word in ofiles.read().split() for word in ylist):
            print "Found"
        else:
            print "Not Found"

您可以将文件read()与文件中的所有单词一起使用。根据你的代码,你总是得到第一行,如果第一行中没有匹配的单词列表,你就会破坏.-

答案 2 :(得分:0)

这会对你有所帮助:

wfiles = ['a.txt', 'b.txt', 'c.txt']                                        
ylist = ['grapes', 'name']                                                  

for w in wfiles:                                                            
    with open(w) as ofiles:                                                 
        content = ofiles.read()                                             
        if any(word in content for word in ylist):                          
            print w, 'FOUND'                                                
        else:                                                               
            print w, 'NOTFOUND'

这对我有用:

import os                                                                      
something = '.'                                                                
ylist = ['grapes', 'name']                                                     
dmd = os.listdir(something)                                                    
wfiles = []                                                                    
for item in dmd:                                                               
    if ".txt" in item:                                                         
        wfiles.append(item)                                                    

for w in wfiles:                                                               
    with open(something + '/' + w) as ofiles:                                  
        content = ofiles.read()                                                
        if any(word in content for word in ylist):                             
            print w, 'FOUND'                                                   
        else:                                                                  
            print w, 'NOTFOUND' 

如果仍然出现意外结果,请检查脚本是否打开了您预期的正确文件。