title: Python: Read in variable number of lines from file based on first character
body: <p>I am trying to read in a file that is composed of groups of data each having a header line.  The number of groups associated with each header can vary.  In the example below you can see that the lines that begin with an 'A' are a header record and the 'B' and 'C' lines are associated with the 'A' record that immediately precedes it.  The second group shows a header record with 4 detail records associated.  Here is an example of the data-</p>
  
  <pre><code>A1001014
  B1234
  C5678
  A10082014
  B1234
  C5678
  B2222
  C2222
  A10072014
  B7777
  C7676
  </code></pre>
  
  <p>How can I get a list for each header record like the following-</p>
  
  <pre><code>[10012014,1234,5678] [10082014,1234,5678] [10082014,2222,2222] [10072014,7777,7676]
  </code></pre>
  
  <p>I've tried several generators functions but I can't seem to figure out how to group the "secondary" (The 3rd list above) records to a previous header record.<br>
  The following is what I came up so far, but it doesn't work when there are more than 2 records per header i.e. it skips the B2222 and C2222. Any help is appreciated!</p>
  
  <pre><code>   def readFile(f):
          for line in f:
            if line[0] == ('1'):  #This isn't shown in the example data below
              procDate=line[23:29] 
  
            if line[0] == ('A'):
              try:
                  line2 = f.next()
                  line3 = f.next()
              except StopIteration:
                  line3 = ''
  
              yield line,line2,line3,procDate
  </code></pre>