speed of string chunks file parsing
MRAB
google at mrabarnett.plus.com
Mon Apr 6 09:57:38 EDT 2009
Hyunchul Kim wrote:
> 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,
>
It looks OK to me.
Of course, it could be the regular expression that's the slowest part.
Have you tried timing it?
More information about the Python-list
mailing list