import sysf=open("grades.csv")grades=f.readlines()f.close()titles = grades.pop(0).split(",")titles.pop(-4)titles[-1] = titles[-1].strip('\n')titles.append("Three lowest scores (Extra Credit)")for student in range(len(grades)): # Turn each line into an array grades[student] = grades[student].split(",") # Unsplit their names grades[student][0] = grades[student][0] + "," + grades[student].pop(1) # Remove newlines grades[student][-1] = grades[student][-1][:-1] # Remove Tot column grades[student].pop(-4) # Add EC column grades[student].append(0)totals_before, totals_after = [],[]for grade in range(len(grades)): # Can't add strings grades[grade][1:] = [int(score) for score in grades[grade][1:]] # Sanity check - each student should have the same number of points # before and after, as we are moving the point mass to the extra # credit column (plus .003) totals_before.append(sum(grades[grade][1:])) # Grab the 24 writeups plus research report copy = grades[grade][:-4] # We want the lowest 3 scores copy.sort() three_lowest = copy[1:4] for low_grade in three_lowest: # Add the score of the dropped grade to EC grades[grade][-1] += low_grade # Replace the first occurences of the students lowest grades # with a very small number grades[grade][grades[grade].index(low_grade)] = .001 totals_after.append(sum(grades[grade][1:]))# Sanity checkfor student in range(len(totals_before)): if not int(totals_before[student]) == int(str(totals_after[student]).split('.')[0]): print "Sanity check failed!" sys.exit(1)# Put the titles backgrades.insert(0, titles)f=open("grades_lowscore_removed.csv","w")for student in grades: for score in range(len(student)): student[score] = str(student[score]) student = ','.join(student) print student f.write(student + "\n")f.close()