[Tutor] Nested loops/test conditions

Gregor Lingl glingl@aon.at
Sun, 10 Feb 2002 20:43:37 +0100


Hi!
I recently had a similar job to do.
I did it with a script like the following:

import string

testFile = 'testtext.txt'

# read filetext into a single string
filetext = open(testFile).read()

# find beginning of first chapter-tag
start = string.find(filetext,'<Chapter')
while start > -1:   # there is at least one more chapter
    # find beginning of chaptertext
    start = string.find(filetext,'>',start) + 1 # for the '>'
    # find end of chaptertext
    end = string.find(filetext,'</Chapter',start)
    chaptext = filetext[start:end] #extracts chaptertext
    print chaptext
    # find beginning of next chapter-tag
    start = string.find(filetext,'<Chapter',end)


So we can avoid joining the ongoing regex-thread ;-)
(but wasn't a similar script there?)
Gregor

----- Original Message -----
From: "Chris Keelan" <rufmetal@rogers.com>
To: <tutor@python.org>
Sent: Sunday, February 10, 2002 7:25 PM
Subject: [Tutor] Nested loops/test conditions


> 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()
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>