slow loop?

Peter Hansen peter at engcorp.com
Mon Jan 13 12:22:52 EST 2003


P at draigBrady.com wrote:
> 
> 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
> >
> > import time
> > startTime=time.time()
> > myList=[]
> > finalList=[]
> > myString=''
> > file=open('Export.txt','r')
> > lines=file.readlines()
> > for line in lines:
> >         for character in line:
> >                 if character == '"':
> >                         None
> >                 elif character == ',' or character == '\n':
> >                         myList.append(myString)
> >                         myString=''
> >                 else:
> >                         myString += character
> >         finalList.append(myList)
> >         myList=[]
> > endTime=time.time()
> > print endTime-startTime
> 
> file=open('Export.txt','r')
> for line in file.readlines():
>      line = line.strip()
>      finalList.append(line.split(','))

This doesn't remove the quotation marks that surround each field, though
it does make a good point: don't process character-by-character in Python
if you want performance!

-Peter




More information about the Python-list mailing list