Python: Using re module to find string, then print values under string
Question: I am attempting to use the re module to search a string of a fairly large
file. The file I am searching has the following format:

    
    
          220
          BOX 1,  STEP 1
          C        15.1760586379       13.7666285127        4.1579861659
          F        13.7752750995       13.3845518556        4.1992254467
          F        15.1122807811       15.0753387163        3.8457966464
          H        15.5298304628       13.5873563855        5.1615910859
          H        15.6594416869       13.1246597008        3.3754112615
            5
         BOX 2,  STEP 1
         C        15.1760586379       13.7666285127        4.1579861659
         F        13.7752750995       13.3845518556        4.1992254467
         F        15.1122807811       15.0753387163        3.8457966464
         H        15.5298304628       13.5873563855        5.1615910859
         H        15.6594416869       13.1246597008        3.3754112615
           240
         BOX 1,  STEP 2
         C        12.6851133069        2.8636250164        1.1788963097
         F        11.7935769268        1.7912366066        1.3042188034
         F        13.7887138736        2.3739304018        0.4126088380
         H        12.1153838312        3.7024696077        0.7164304431
         H        13.0962656950        3.1549047758        2.1436863477
         C        12.6745394723        3.6338848332       15.1374252921
         F        11.8703828307        4.3473226569       16.0480492173
         F        12.2304604843        2.3709059503       14.9433964493
         H        12.6002811971        4.1968554204       14.1449118786
         H        13.7469256153        3.6086212350       15.5204655285
    

This format continues on for Box 1 and Box 2 for ~30000 STEPS total, for each
BOX. I have code that utilizes the re module to searches this file based on
the keyword "STEP". Unfortunately, it does not yield any results when I run
it. I need my code to search 1) for **ONLY** Box 1, then 2) print/output all
the coordinates(preferably omitting the "C's, F's, H's"; so only the
coordinates) beginning after STEP 1 to a file, 3) increment the "STEP" number
by 48 and then repeat 2). I also want to ignore the "5" and the "240" in the
file that I am searching; so the code should compensate so that this is not
included in the output after we search this file. This is what I have thus far
(it does not work):

    
    
     import re
     shakes = open("mc_coordinates", "r")
     i = 1
     for line in shakes:
            if re.match("(.*)STEP i(.*)", line):
                   print line
            i+=48
    

This is an example of what I what my code to do:

    
    
      STEP 1
        15.1760586379       13.7666285127        4.1579861659
        13.7752750995       13.3845518556        4.1992254467
        15.1122807811       15.0753387163        3.8457966464
        15.5298304628       13.5873563855        5.1615910859
        15.6594416869       13.1246597008        3.3754112615  
      STEP 49
        12.6851133069        2.8636250164        1.1788963097
        11.7935769268        1.7912366066        1.3042188034
        13.7887138736        2.3739304018        0.4126088380
        12.1153838312        3.7024696077        0.7164304431
        13.0962656950        3.1549047758        2.1436863477
        12.6745394723        3.6338848332       15.1374252921
        11.8703828307        4.3473226569       16.0480492173
        12.2304604843        2.3709059503       14.9433964493
        12.6002811971        4.1968554204       14.1449118786
        13.7469256153        3.6086212350       15.5204655285
      STEP 97
        15.1760586379       13.7666285127        4.1579861659
        13.7752750995       13.3845518556        4.1992254467
        15.1122807811       15.0753387163        3.8457966464
        15.5298304628       13.5873563855        5.1615910859
        15.6594416869       13.1246597008        3.3754112615  
    

It should be noted that this is a condensed version, typically there will be
~250 lines of coordinates in between "STEP" numbers. Any ideas or thought will
be appreciated. Thanks!!


Answer: A quick although maybe not efficent way is to just parse line by line and add
some states.

    
    
    # untested code, but i think you get the idea
    import re
    shakes = open("mc_coordinates", "r")
    i = 1
    output = False # are we in a block that should be output?
    for line in shakes:
        if re.match("(.*)STEP i(.*)", line): # tune this to match only for BOX 1
            print line
            output = true
            i+=48
        elif re.match("(.*)STEP i(.*)", line):
            # some other box or step
            output = false
        elif output:
            print line # or remove the first few chars to get rid of C,F or Hs.
    

