在Python中将文件列表的内容与单词列表进行对比

2024-03-29 14:00:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试找出如何对照单词列表(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

我做错什么了?你知道吗


Tags: theintxt内容列表foristhis
3条回答

这条线:

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
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"

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

这将帮助您:

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' 

如果仍然得到意外结果,请检查脚本是否打开了预期的正确文件。你知道吗

相关问题 更多 >