speed of string chunks file parsing

Hyunchul Kim sundol at sfc.keio.ac.jp
Mon Apr 6 09:48:51 EDT 2009


Hi, all

I have a simple script.
Can you improve algorithm of following 10 line script, with a view point 
of speed ?
Following script do exactly what I want but I want to improve the speed.

This parse a file and accumulate lines till a line match a given regular 
expression.
Then, when a line match a given regular expression, this function yield 
lines before the matched lines.

****************
import re
resultlist = []
cp_regularexpression = re.compile('^a complex regular expression here$)
for line in file(inputfile):
        if cp_regularexpression.match(line):
                if resultlist != []:
                        yield resultlist
                        resultlist = []
        resultlist.append(line)
yield resultlist
****************

Thank you in advance,

Hyunchul





More information about the Python-list mailing list