[Tutor] how to extract data only after a certain condition is met
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
> 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
read line from file
sentinel = checkLine(line, startPattern, endPattern)
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.
Author of the Learn to Program web site
More information about the Tutor