slow loop?

Brian Kranson bk at whack.org
Mon Jan 13 17:42:34 CET 2003


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


Here are two lines from Export.txt (the first line ends with "NONE" -
google and/or your browser might add some extra end of line
characthers - this sample Export.txt should only have two lines)

"IDNUMBER","VRNUMBER","LASTNAME","FIRSTNAME","MAILNAME","SALUTATION","ADDRESS1","ADDRESS2","ADDRESS3","CITY","STATE","ZIP","COUNTY","CONGRESS","SENATEDIST","HOUSEDIST","RECSTATUS","REGISTER","CARRT","HPHONE","WPHONE","FPHONE","APHONE","EMPLOYER","OCCUPATION","BIRTHDATE","CONSTITUENTTYPE","ORIGIN","AFFILIATION","SEX","PRECINCT","HOMESOUND","UPDATEDATE","FAMILY","CONTEXIST","EXPEXIST","VOTEEXIST","N
TEEXIST","VOLEXIST","VIPEXIST","CASEEXIST","REMIND","BILLEXIST","WFAX_PHONE","CAR_PHONE","E_MAIL","PAGER","MAIL_WHERE","CONTACT","AWARD_TYPE","AWARD_DATE","ELIGIBLE","JOIN_DATE","PAY_GRP","PAY_AMOUNT","WADDRESS1","WADDRESS2","WADDRESS3","WCITY","WSTATE","WZIP","WCARRT","MADDRESS1","MADDRESS2","MADDRESS3","MCITY","MSTATE","MZIP","MCARRT","DADDRESS1","DADDRESS2","DADDRESS3","DCITY","DSTATE","DZIP","
CARRT","HOUSEHOLDMAILNAME","HOUSEHOLDSALUTATION","TOTALCONTRIBUTIONS","LARGESTCONTRIBUTION","AVERAGECONTRIBUTION","LASTCONTRIBUTION","LASTCONTRIBUTIONDATE","YEAR1TOTAL","YEAR2TOTAL","CONTRIBUTIONSYEAR1","CONTRIBUTIONSYEAR2","NONE"
"63","","Last","First","Ms. First L. Last","Sal","220 Street
Blvd.","","","Oregon","OH","43616","Lucas","9","","","1","  /  /   
","","","","","","","","12/30/1899","Individual","","","Female","","HOME","07/13/1999","","Y","","","","","","","","","","","","","H","","","
 /  /    ","","  /  /    ","","0","220 Street
Blvd.","","","Oregon","OH","43616","","220 Street
Blvd.","","","Oregon","OH","43616","","220 Street
Blvd.","","","Oregon","OH","43616-","","Ms. Frist L.
Last","First","25.00","25.00","25.00","25.00","01/06/1999","0.00","0.00","0","0","none"




More information about the Python-list mailing list