slow loop?

P at draigBrady.com P at draigBrady.com
Mon Jan 13 17:45:31 CET 2003


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(','))

Pádraig.





More information about the Python-list mailing list