[英]Extract specific words from the file
I am analyzing some text files and I want to extract a specific word every time that the word is found in the file.我正在分析一些文本文件,每次在文件中找到该词时,我都想提取一个特定的词。
Imagine that I have 'Sports' in the file then I want to extract the Word 'SPORTS' based on a list.想象一下,我在文件中有“体育”,然后我想根据列表提取单词“体育”。
I've the following code:我有以下代码:
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)
My text file has this content:我的文本文件有以下内容:
Sports TV is the home of football videos.
Complex game to follow.
home of football
With my code I print all the lines with 'Sports' and 'Football':用我的代码打印所有带有“体育”和“足球”的行:
Sports TV is the home of football videos.
home of football
But I want to see the following result:但我想看到以下结果:
Sports
football
How can I print only the word that I have on List instead of all the line?如何仅打印 List 上的单词而不是所有行?
Thanks!谢谢!
list.txt:列表.txt:
Sports TV is the home of football videos.
Complex game to follow.
home of football
Hence :因此:
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])
OUTPUT :输出:
[['sports', 'football'], [], ['football']]
Since :由于:
line 1 had
sports
andfootball
1号线有
sports
和football
line 2 had no matching elements from content list
第 2 行没有来自内容列表的匹配元素
line 3 had
football
3号线有
football
you are printing the entire line at the moment您目前正在打印整行
try:尝试:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.