0

For example if I have txt file with content:

12 15 33 40
9 23 44 45
22 26 76 45
9 23 44 45

I want to remove the repeated line which in this case is 9, 23, 44, 45 and replace it with only one line instead of two. i.e. I want the output to be:

12 15 33 40
9 23 44 45
22 26 76 45

I tried this but it didn't work:

from itertools import groupby
with open('result.txt', 'r') as infile:
    with open('final_result.txt', 'w') as outfile:
        for line, _ in itertools.groupby(infile):
            outfile.write(line)

Finally this works:

lines_seen = set() # holds lines already seen
outfile = open('final_result.txt', "w")
for line in open('result.txt', "r"):
    if line not in lines_seen: # not a duplicate
        outfile.write(line)
        lines_seen.add(line)
outfile.close()

0 Answers0