[Tutor] how to extract data only after a certain condition is met
Alan Gauld
alan.gauld at btinternet.com
Wed Oct 6 20:18:09 CEST 2010
"Eduardo Vieira" <eduardo.susan at gmail.com> wrote
> The other day I was writing a script to extract data from a file
> from
> the line where a text is found to the end of the file.
The standard pattern here is to use a sentinel, in pseudo code:
def checkLine(line, start='',end=''):
if (start in line) or (end in line): return True
else: return False
startPattern = 'some string (or regex)'
endPattern = 'a concluding string or regex'
sentinel = False
while True
read line from file
sentinel = checkLine(line, startPattern, endPattern)
if sentinel:
processLine(line)
You can simplify or complexify that in many ways, and you can
add a break check to speed it up if you only expect to process
a few lines.
And checkLine can be as simple or as complex as you like.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list