split and regexp on textfile

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Apr 15 17:03:33 EDT 2007


En Fri, 13 Apr 2007 07:08:05 -0300, Flyzone <flyzone at technologist.com>  
escribió:

> A little question: the pat.split can split without delete the date?

No, but instead of reading the whole file and splitting on dates, you  
could iterate over the file and detect block endings:

def split_on_dates(ftoparse):
   block = None
   for line in ftoparse:
     if fancy_date_regexp.match(line):
       # a new block begins, yield the previous one
       if block is not None:
         yield current_date, block
       current_date = line
       block = []
     else:
       # accumulate lines for current block
       block.append(line)
   # don't forget the last block
   if block is not None:
     yield current_date, block

for date, block in split_on_dates(ftoparse):
   # process block

-- 
Gabriel Genellina



More information about the Python-list mailing list