-2

My keywords

keywords = ['monday', 'tuesday', 'wednesday', 'thursday']

My txt file content: Today is tuesday and tomorrow is wednesday

Expected Output should be:

tuesday wednesday

Amit Soni
  • 3,216
  • 6
  • 31
  • 50
lol
  • 11
  • 1
  • 4

2 Answers2

1

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)
1

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

Athul Soori
  • 91
  • 1
  • 8