从文件中提取特定单词

时间:2019-04-02 15:09:34

标签: python text read-text

我正在分析一些文本文件,因此我希望每次在文件中找到一个单词时都提取一个特定的单词。

想象一下我在文件中有“体育”,然后我想根据列表提取单词“体育”。

我有以下代码:

content = ['Sports', 'Nature', 'Football']
path = filename
with open(path) as auto:
    for line in auto:
        if any(x.lower() in line.lower() for x in content):
            print(line)

我的文本文件具有以下内容:

Sports TV is the home of football videos. 
Complex game to follow.
home of football

使用我的代码,我将“ Sports”和“ Football”的所有行打印出来:

Sports TV is the home of football videos. 

home of football

但是我想看到以下结果:

Sports
football

如何仅打印列表中的单词而不是所有行?

谢谢!

2 个答案:

答案 0 :(得分:1)

list.txt:

Sports TV is the home of football videos. 
Complex game to follow.
home of football

因此

content = ['Sports', 'Nature', 'Football']
path = 'list.txt'

with open(path) as auto:
    print([[x.lower() for x in content if x.lower() in line.lower()] for line in auto])

输出

[['sports', 'football'], [], ['football']]

开始:

  

第1行有sportsfootball

     

第2行的内容列表中没有匹配的元素

     

第3行有football

答案 1 :(得分:0)

您现在正在打印整行

尝试:

content = ['Sports', 'Nature', 'Football']
path = filename
with open(path) as auto:
    for line in auto:
        for x in content:
            if x.lower() in line.lower():
                print(x)
相关问题