Newbie Question: Regular Expressions

Skip Montanaro skip at pobox.com
Thu Jul 12 13:09:45 EDT 2001


    Daniel> I have a really dumb program that i would like to make smarter.
    Daniel> I need to take a file on my hard drive and filter out everything
    Daniel> except for the standings which are written in it.  I have tried
    Daniel> to use regular expressions with no success, but i still think
    Daniel> that they are probably the best way. 

Assuming "League Standings" is the marker, try something like this:

    sawstandings = 0
    f = open('rawdata', 'r')
    for line in f.readlines():
      if line.strip() == "league standings":
        print line,
        sawstandings = 1
      elif sawstadings:
        print line,

No regular expressions needed.  Obviously, if the league standings aren't
the last thing in the file you will need a similar test to decide when to
break out of the loop.

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list