slow loop?
P at draigBrady.com
P at draigBrady.com
Mon Jan 13 12:25:21 EST 2003
Peter Hansen wrote:
> 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!
sorry forgot about that, something like the following should work:
finalList=[]
file=open('Export.txt','r')
for line in file.readlines():
line = line.strip().replace('"','')
finalList.append(line.split(','))
Pádraig.
More information about the Python-list
mailing list