Look at the following code:
infile = r"D:\Documents and Settings\xxxx\Desktop\test_log.txt"
important = []
keep_phrases = ["test",
"important",
"keep me"]
with open(infile) as f:
f = f.readlines()
for line in f:
for phrase in keep_phrases:
if phrase in line:
important.append(line)
break
print(important)
It's in no way, shape, or forms great, for instance, there is no exception handling or pattern matching, however, you can add these to it without any problem. Look into regular expression, which might be superior to express coordinating. On the off chance that your documents are extremely huge, read it line by line to keep away from a MemoryError.
Input file:
This line is super important!
don't need this one...
keep me!
bla bla
not bothered
ALWAYS include this test line
Output:
['This line is super important!\n', 'keep me!\n', 'ALWAYS include this test line']
PS: This is Python 3.3
For more details, refer to this video tutorial on,