slow loop?

P at P at
Mon Jan 13 18:25:21 CET 2003

Peter Hansen wrote:
> P at 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
>>>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=[]
>>>print endTime-startTime
>>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!

sorry forgot about that, something like the following should work:

for line in file.readlines():
      line = line.strip().replace('"','')


More information about the Python-list mailing list