Try to search your question here, if you can't find : Ask Any Question Now ?

Filter on exact word in multiple lines of text file

HomeCategory: stackoverflowFilter on exact word in multiple lines of text file
Avatarjohn asked 4 weeks ago

I have a text file that I’m reading.
I want to filter these lines
Example lines

5.254.739   478.661 P   PPL     NL      7   361
5.241.535   467.464 P   PPL     NL      7   392
5.238.084   463.683 P   PPLA    NL      7   392
5.260.252   468.815 P   PPLA2   NL      7   399

I want to filter only on ‘PPL’ so the rest should not be included

I tried it with code below
I also tried to apply regex but I keep failing in it

with open("source.txt", "r") as f:
    with open("new.txt", "w") as output:
        new_f = f.readlines()
        f.seek(0)
        for line in new_f:
            if "PPL" in line:
                output.write(line)

I would like to be able to filter the lines containing the exact word
When filter on PPL the other lines like PPLA and PPLA2 should be ignored

1 Answers
Best Answer
AvatarAmit answered 4 weeks ago
Your Answer

10 + 0 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com