It is necessary to implement a function in which an array containing several words (the number is unknown) will be passed as a parameter. There is a source file, output.txt, it contains a large number of lines, it is necessary for the function to delete all the lines entirely if the line does not contain at least one word from the array of words
This is what I have at the moment, but this function only removes one word at a time, but it needs several
import re
with open('output.txt') as f:
lines = f.readlines()
str = "bmw" # Keyword
pattern = re.compile(re.escape(str))
with open('output.txt', 'w') as f:
for line in lines:
result = pattern.search(line)
if result is not None:
f.write(line)