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 ie 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)
I think you want to yield previous
, rather than value
:
def uniq(iterator):
previous = None
section = ("SECTION : MISSING\n")
for value in iterator:
if previous == value:
yield section
elif previous is not None:
yield previous
previous = value
if previous is not None:
yield previous
Example usage:
>>> list(uniq([1, 2, 2, 3, 4, 5, 6, 6]))
[1, 'SECTION : MISSING\n', 2, 3, 4, 5, 'SECTION : MISSING\n', 6]
Something like:
prev = None
with open('infile.txt','r') as fi:
with open('outfile.txt','w') as fo:
for line in fi:
if prev is not None:
fo.write(prev if prev != line else "SECTION : MISSING\n")
prev = line
fo.write(prev)
Will give you the output file you're looking for:
line1 line2 SECTION : MISSING line3 line4 line5 line6
Personal preference for tasks like these, I use two cursors instead of one:
from itertools import tee, izip
with open(infile) as r, open(outfile, 'w') as w:
p, c = tee(r)
w.write(next(c))
for prev,cur in izip(p,c):
w.write(cur if prev!=cur else 'SECTION : MISSING\n')
In case you ever have to handle the situation with three consecutive lines (well, two or more) where you only want to replace the first one, you could use groupby
:
from itertools import groupby, islice, chain
def detect_missing(source):
grouped = groupby(source)
section = "SECTION: MISSING\n"
for _, group in grouped:
first_two = list(islice(group, 2))
if len(first_two) > 1:
first_two[0] = section
yield from chain(first_two, group)
(Python 3, but you could remove the yield from
if you wanted.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.