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?
Thanks!
list.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
line 2 had no matching elements from content list
line 3 had
football