Parsing

Anton Muhin antonmuhin at sendmail.ru
Thu Apr 17 13:25:45 CEST 2003


Or something like this:

from __future__ import generators

def aggr(seq, n):
     t = []
     for e in seq:
         t.append(e)
         if len(t) == n:
             yield t
             t = []
     if t:
         yield t + [None * (4 - len(t))]

f = file("e:/1.txt")

for section, num0, num1, _ in aggr(f, 4):
     print section.rstrip(), num0.rstrip(), num1.rstrip()

f.close()

BTW, if I'm correct modern itertools module has analog (much more 
elaborated, I suppose) for aggr.

hth,
anton.





More information about the Python-list mailing list