My keywords
keywords = ['monday', 'tuesday', 'wednesday', 'thursday']
My txt file content: Today is tuesday and tomorrow is wednesday
Expected Output should be:
tuesday wednesday
My keywords
keywords = ['monday', 'tuesday', 'wednesday', 'thursday']
My txt file content: Today is tuesday and tomorrow is wednesday
Expected Output should be:
tuesday wednesday
Try following. It will open the file and read it line by line. Each key word will be checked in against line whether it exists or not. You can also use intersection of the sets.
for line in open('file.txt'):
for k in keywords:
if k in line:
print(k)
You can use regex to check whether the keywords are in the in text or not.
import re
keywords=['monday','tuesday','wednesday','thursday','friday']
with open('text.txt') as f:
txt=f.read()
for i in keywords:
if re.search(r'\b{}\b'.format(i),txt):
print i
The resulting output would be : tuesday wednesday