Lecture 39:

posted Apr 11, 2013, 11:40 AM by Samuel Konstantinovich   [ updated Apr 11, 2013, 11:41 AM ]
Your exam is tomorrow.

The code created in class is not as important as how it was created. Taking notes about the reasoning and the steps is more important. 

#GOALS:
#Demonstrate good programming practices,
#Break down a complicated problem into small steps

infile=open('SAT2010.csv','r')
outfile=open('new.csv','w')
outfile.write("School Name,Total Score\n")

allLines=infile.readlines()
infile.close()

header = allLines[0].split(',')
nameIndex = header.index('School Name')
mathIndex = header.index('Mathematics Mean')
readIndex = header.index('Critical Reading Mean')
writIndex = header.index('Writing Mean\n')

for line in allLines[1:]:    
    commaIndex = line.find(',')
    if line[commaIndex+1]=='"':
        print "OOPS! Bad data (not a bad school...)"
        print line
    else:
        nextSchool = line.split(',')
        name = nextSchool[nameIndex]
        writeBuffer= name + ","
        
        if nextSchool[readIndex]!='s':    
            read = int(nextSchool[readIndex])
            math = int(nextSchool[mathIndex])
            writ = int(nextSchool[writIndex])
            writeBuffer+= str(read+math+writ)
        else:
            writeBuffer+='s'

        outfile.write(writeBuffer+'\n')
outfile.close()

The data file is attached:
ċ
SAT2010.csv
(25k)
Samuel Konstantinovich,
Apr 11, 2013, 11:40 AM
Comments