Martin
Martin

Reputation: 229

Searching for strings in txt file

I have multiple .txt files and I'm looking for any of such strings: lst = ['PASSED', 'FAILED', 'PASSED WITH WARNINGS'] I crated code, but it splits on whitespaces and I get just PASSED instaead of PASSED WITH WARNINGS. How to correct my code?

for i in range(len(tab)):
    with open(tab[i], 'r') as infile:
        lines = infile.readlines()
        var = [line for line in lines if (set(line.split()) & set(msgs))]
        print(var)

Example of my .txt file:

Some info here
Another info
Some more info
And so on...
2021_09_09_09_09_31     PASSED WITH WARNINGS
2021_09_09_09_09_34     PASSED WITH WARNINGS

Upvotes: 0

Views: 50

Answers (1)

Kevin Eaverquepedo
Kevin Eaverquepedo

Reputation: 652

lst = ['PASSED', 'FAILED', 'PASSED WITH WARNINGS']
for i in range(len(tab)):
    with open(tab[i], 'r') as infile:
        lines = infile.readlines()
        var = [line.split(" ", 1)[-1].strip() for line in lines if any([word in line for word in lst])]
        print(var)

Upvotes: 1

Related Questions