[Tutor] Nested loops/test conditions

Chris Keelan rufmetal@rogers.com
Sun, 10 Feb 2002 13:25:02 -0500


Hey gang!

I know the answer is staring me in the face, but I've been scratching
my head over this problem for two days.

I want to parse a file that has the following structure:

################

Some header stuff to skip

<Chapter1>

Some lines of text to write

</Chapter1>

<Chapter2>

Some more lines of text to write

</Chapter2>

etc. ...

################

So I need to write two loops, I think. The first loop should parse the
file line-by-line to find the start condition e.g., <Chapter1> then
write each line from that point to the end condition </Chapter1> to a
separate file.

I then want a third, meta-loop to iterate thorough a list of all the chapters.

So the program loops over a long text file and writes a chapterized
version of that document based on start and end markers within the
file itself.

This is the best I can do, but it's not doing the second (nested) iteration
properly (I'll worry about the meta-loop later). I'm also not worried about
writing to file yet--just getting the program to print the output to screen
will make me happy:

#!/usr/bin/python

import os
import string

startCond = '<Chapter1>'

exitCond = '</Chapter1>'

testFile = '/shared/docs/testtext'


def fix_string(badString):     # need this to strip newlines, etc. from a
readline()    goodString = string.split(badString)
    goodString = string.join(goodString)
    return goodString

def find_start(inHandle, startCond, exitCond):

    done = 0

    while not done:
        checkLine = inHandle.readline()

        testCond = fix_string(checkLine)

        if testCond == startCond:
            print_lines(inHandle, exitCond)

        else:
            done = 1
            
def print_lines(inHandle, exitCond):
    done = 0
    while not done:
        checkLine = inHandle.readline()

        testCond = fix_string(checkLine)
        
        if testCond == exitCond:
            done = 1
                       
        else:
            print checkLine
            
            
def main():
    inHandle = open(testFile, 'r')
    find_start(inHandle, startCond, exitCond)
    inHandle.close()

if __name__ == "__main__":
    main()