I have the following input file structure, with text on each line :
line1
line2
line3
line3
line4
line5
line6
When two lines are exactly the same i.e. line 3 I want to keep the second one and change the content of the first to be "SECTION MISSING". I do not manage to put it at the right place. The closest I get to is with the code below but the output I get is :
line1
line2
line3
SECTION MISSING
line4
etc.
While I want:
line1
line2
SECTION MISSING
line3
line4
Code:
def uniq(iterator):
previous = float("NaN") # Not equal to anything
section=("SECTION : MISSING\n")
for value in iterator:
if previous == value:
yield section
else:
yield value
previous = value
return;
with open('infile.txt','r') as file:
with open('outfile.txt','w') as f:
for line in uniq(file):
f.write(line)