slow loop?

Mike C. Fletcher mcfletch at rogers.com
Mon Jan 13 12:05:15 EST 2003


You should really tell us what you are trying to do, as that seems like 
a pretty strange result to want (all of the strings, but going from 
after-string-start to either end-of-line or ',').  I'm guessing you're 
wanting something like: all of the "" quoted strings on each line. 
 Normally you'd use something like the comma-seperated-value module to 
load this kind of stuff, but if you want to see a minimal example:

import re
finder = re.compile( r'".*?"')

def getstrings( filename ):
    result = []
    for line in open(filename, 'r'):
        result.append( finder.findall( line ) )
    return result

I didn't test that, and it definitely doesn't handle escapes (i.e. \" 
inside a string), but that's a whole other realm of pain :) .  I'd guess 
the above'll be faster than looping over each character in Python.

HTH,
Mike  

Brian Kranson wrote:

>Is there a way I can make this small script any faster?  The file it
>reads in used to be only about a 100 lines and now it is well over
>2000.  It takes about 14 seconds to run it on my PentiumII.  Thanks in
>advance - Bk
>  
>
...







More information about the Python-list mailing list