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:
Hence:
OUTPUT:
Since: