0

I'm currently developing a code which sorts the data from a csv file according to the user's choice.

At the moment I'm having trouble as my sorting for highest score doesn't really work. It recognizes 10 as 010 and therefore infers it as less than the other numbers which is 09 etc. Is there any way I could fix this ?

http://postimg.org/image/onfbhpaxn/ This is the result I currently get at the moment

This is the code I use to workout the highest score.

high = max(int(x) for x in row[2:4]) # finds the highest score from the row 2:4
high = str("0") + str(high) # adds a 0 in front of the high score so that it could be used for sorting
row.insert( 6, high) #insert the high value on row 6
write.writerows([row[:7]])

This is the code I use to sort.

 if choice == 2: #if choice is 2
     form = input("Enter a class to sort it's Data: ") 
     filename = 'class{}.csv'.format(form)
     print("• Enter 1 to sort by student's highest score in alphabetical order. ")
     print("• Enter 2 to sort by student's highest score (highest to lowest)")
     print("• Enter 3 to sort by Student's average score (highest to lowest)")
     sorttype = int(input("Choose a option from above to sort: "))  # ask the teacher to choose an option for the sorting from above
     print("                                                                                       ")  #prints out nothing (space ) so that the sorted data is layed out in a neater way 
     with open (filename,'r', newline='') as keerthan: #open the file to read using csv writer
         read = csv.reader(keerthan,delimiter=',') #creates a function to read lines in the sort file
         if sorttype == 1: sorting = sorted(read, key=operator.itemgetter(0,6)) # if the student choses 1 ; sort it in alphabetical order
         elif sorttype == 2:sorting = sorted(read, key=operator.itemgetter(6,0), reverse = True) #if choice 2 , sort it is lowest to highest and then reverse.
         elif sorttype == 3:sorting = sorted(read, key=operator.itemgetter(5,0), reverse = True) # if choice 3 , sort by average score ; lowest to highest and then reverse.
         for row in sorting: # for every row in the file
             if row[0].startswith("Name"):None #if the first column is "name" (like the header): skip it

             else: #else (otherwise)
                 if sorttype == 3: print(row[0] + ' ' + row[1] + ': ' + row[5]) #otherwise print FirstName, SecondName and average score

                 else:print(row[0] + ' ' + row[1] + ': ' + row[6])

Thank you.

arvi1000
  • 9,393
  • 2
  • 42
  • 52

1 Answers1

0

Since what you want to do is sort based on numbers, you should have the key function convert the values you want to integers. Here's an example to get you started:

values = [('Alice', '80'), ('Bob', '5'), ('Chuck', '80')]
sorted(values, key = lambda row: (-int(row[1]), row[0]))
Julian
  • 2,483
  • 20
  • 20
  • Thanks for the help , so instead of this if sorttype == 1: sorting = sorted(read, key=operator.itemgetter(0,6)) i should use what u provided ?. Or are u suggesting that I change the values into integers before I sort ? – chiefkeef12 May 04 '15 at 17:30
  • The first sort works the way you intended right? I was suggesting changing the second and third sorts to sort based on the integer values rather than the strings. – Julian May 04 '15 at 17:34
  • Yes it does. And ahh i see , im not really familiar with lambda :/ but I will try it to see what happens. Thanks – chiefkeef12 May 04 '15 at 17:38
  • Hello , so i did this elif sorttype == 2:sorting = sorted(read, key = lambda row: -int(row[1], reverse = True)) instead of my original sorting for the highest score. However i got an error. http://postimg.org/image/6dm1a23yz/ Also , is my sorting right ? or did i do something stupid ? http://postimg.org/image/w4gw82n45/ – chiefkeef12 May 04 '15 at 17:42
  • So `lambda` is just a way to quickly define a function, and the `key` argument expects a function. What you want is a function that returns the value you actually want to sort on. `operator.itemgetter` is a good option when you just want to do a simple sort on a value. Try to write a function that takes a row of your data and returns the value you want to sort on! – Julian May 04 '15 at 17:47
  • Ahh i see , thanks and would read = csv.reader(keerthan,delimiter=',') be alright ? I used that to read my csv file so i can sort it. – chiefkeef12 May 04 '15 at 18:07