I have eliminated some of the nested loops for simplicity of the example.
I am iterating over a file line-by-line using fileinput. If the line meets a certain condition I want it to replace all future lines with '' until it meets the condition again.
import re
import fileinput
with fileinput.FileInput("survey.qsf", inplace=True, backup='.bak') as file:
for line in file:
if re.match(r'l'+datamap[i][2]+'.*$',line)!=None:
line=re.sub(r'.*$','',line)
while re.match(r'lQID\d*$',line)==None:
line=re.sub(r'.*$','',line)
next(line)
I used "next(line)" as a placeholder as I can't figure out how to iterate to the next line without breaking out of the inner loop.
I want to be able to iterate through the lines to have:
lQID44
xyz
xyz
lQID45
output as:
[blank line]
[blank line]
[blank line]
lQID45
Thanks.
str.startswithrather thanre, as imo that would suit here too. However, perhaps i just didn't get the complicated point here....next(file)gives you the next line of the iterator. Beware that using it like this can lead to aStopIterationexception, if there is no next line, so maybe you should guard your call ofnextwith a try-except-block, if you cannot make sure that there will always be a next line.