【问题标题】:Checking the content of a list of files against a list of words in Python根据 Python 中的单词列表检查文件列表的内容
【发布时间】: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


【解决方案1】:
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()。根据你的代码,你总是得到第一行,如果第一行中的单词列表不匹配,你会中断。-

【讨论】:

  • 感谢您的澄清!然而奇怪的是,所有三个文件都打印了“找到”,知道为什么吗?
  • 是的..现在此实现基于单个文件工作...您可以编辑 print "Found in",w 以识别在哪个文件中找到了这些密钥。
  • 收到Found in a.txt ... Found in c.txt',共打印三行(每个文件一行)。这是我在问题中使用的(print w, 'FOUND')(我现在将添加一个输出示例)。
  • 你在 b.txt 和 c.txt 中没有“葡萄”或“名字”吗?
  • 它们都不存在;这两个文件的内容现在都包含在问题中。
【解决方案2】:

这一行:

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

【讨论】:

  • 奇怪的是输出保持不变。在for xlist in ofiles.read().split(): 下插入print xlist 发现它只是抓取每个.txt 文件中的第一个单词。
  • 是的,这用于迭代单个单词。如果您想保留代码原样,请更改此行:if any(word in xlist for word in ylist):if any(word in xlist.read().split() for word in ylist):
  • 这似乎是对的。奇怪的是,仍然为所有三个文件打印了“FOUND”。
  • 现在将它们添加到问题中。
  • 奇怪的是输出对我来说是一样的,虽然可能是因为我遗漏了绝对不应该的代码。现在添加了它,你能看到任何可能影响我没有收到正确输出的东西吗?
【解决方案3】:

这会对你有所帮助:

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' 

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

【讨论】:

  • 奇怪的是 'FOUND' 仍然为所有三个文件打印出来(尝试使用你的两个示例)。
  • 您可以发布您的 b.txt 以供参考吗?
  • 对我来说奇怪的是相同的,尽管可能是因为我遗漏了绝对不应该的代码。现在添加它,你能看到任何可能影响我没有收到正确输出的东西吗?
猜你喜欢
  • 1970-01-01
  • 2013-09-12
  • 2020-09-11
  • 2013-02-02
  • 2015-05-28
  • 2014-05-07
  • 2021-07-11
  • 1970-01-01
  • 2019-04-24
相关资源
最近更新 更多