从文件中提取特定的单词

Extract specific words from the file

我正在分析一些文本文件,每次在文件中找到该词时我都想提取该词。

假设我在文件中有 'Sports' 然后我想根据列表提取单词 'SPORTS'。

我有以下代码:

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

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

谢谢!

你现在正在打印整行

尝试:

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)

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']]

:

line 1 had sports and football

line 2 had no matching elements from content list

line 3 had football