title: processing stdin data blocs with python and groupby
body: <p>The objective is to have a python script process sorted <code>stdin</code> line by line, collect 'data blocs' having the same key, and then output that bloc as JSON (or any other single object) once the key changes. At this stage I just want to be able to isolate key &amp; data blocs by using <code>groupby</code> and <code>itertools</code> correctly. In the example below, there are two blocs of three rows each. The setup is</p>
  
  <pre><code> cat data.csv | python qwe.py
  </code></pre>
  
  <p>where <code>data.csv</code> is</p>
  
  <pre><code> 1,this,is
   1,a,test
   1,hope,it
   2,works,well
   2,and,I
   2,move,forward
  </code></pre>
  
  <p>and <code>qwe.py</code> is</p>
  
  <pre><code> import itertools
   import operator
   import sys
   import re
  
   def main(argv):
    for key, bloc in itertools.groupby( sys.stdin.readline().split(',') ,
                                   key=operator.itemgetter(0)):
        print '-----' + key
        print list(bloc)
  
   if __name__ == "__main__":
    main(sys.argv)
  </code></pre>
  
  <p>Thanks for any hints.</p>