<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    On 4/20/2013 1:09 PM, Jason Friedman wrote:<br>
    <blockquote
cite="mid:CANy1k1jVD3xDQGVdGRuCycg4yXojtqKfAzZFM=C88SsHnqrLQQ@mail.gmail.com"
      type="cite">
      <div dir="ltr">I have a file such as:
        <div><br>
        </div>
        <div>
          <div>$ cat my_data </div>
          <div>Starting a new group<br>
          </div>
          <div>a</div>
          <div>b</div>
          <div>c</div>
          <div>Starting a new group</div>
          <div>1</div>
          <div>2</div>
          <div>3</div>
          <div>
            4</div>
          <div>Starting a new group</div>
          <div>X</div>
          <div>Y</div>
          <div>Z</div>
          <div>Starting a new group</div>
        </div>
        <div><br>
        </div>
        <div style="">I am wanting a list of lists:</div>
        <div style="">['a', 'b', 'c']<br>
        </div>
        <div style="">['1', '2', '3', '4']</div>
        <div style="">['X', 'Y', 'Z']</div>
        <div style="">[]</div>
        <div style=""><br>
        </div>
        <div style="">I wrote this:</div>
        <div style="">
          <div>------------------------------------</div>
          <div>#!/usr/bin/python3</div>
          <div>from itertools import groupby</div>
          <div><br>
          </div>
          <div>def get_lines_from_file(file_name):</div>
          <div>    with open(file_name) as reader:</div>
          <div>        for line in reader.readlines():</div>
          <div>            yield(line.strip())</div>
          <div><br>
          </div>
          <div>counter = 0</div>
          <div>def key_func(x):</div>
          <div>    if x.startswith("Starting a new group"):</div>
          <div>        global counter</div>
          <div>        counter += 1</div>
          <div>    return counter</div>
          <div><br>
          </div>
          <div>for key, group in groupby(get_lines_from_file("my_data"),
            key_func):</div>
          <div>    print(list(group)[1:])</div>
          <div>------------------------------------<br>
          </div>
        </div>
        <div style="">
          <div><br>
          </div>
          <div style="">I get the output I desire, but I'm wondering if
            there is a solution without the global counter.</div>
        </div>
        <div style=""><br>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
    </blockquote>
    <br>
    def separate_on(lines, separator):<br>
        group = None<br>
        for line in lines:<br>
            if line.strip() == separator:<br>
                if group is not None:<br>
                    yield group<br>
                group = []<br>
            else:<br>
                assert group is not None # Should have gotten a
    separator first<br>
                group.append(line)<br>
        yield group<br>
    <br>
    with open("my_data") as my_data:<br>
        for group in separate_on(my_data, "Starting a new group"):<br>
            print group<br>
    <br>
    <br>
    The handling of the first separator line feels delicate to me, but
    this provides the output you want.<br>
    <br>
    --Ned.<br>
  </body>
</html>