I have this piece of code written to sort a csv file in Python.
import csv
from operator import itemgetter
reader = csv.reader(open("highscores.txt"), delimiter=",")
sortedList = sorted(reader, key=itemgetter(1), reverse=True)
print(sortedList)
I am trying the sort the file by the second item in the array, for example if in my file I had:
Callum,22
Kim,43
It would sort it to:
Kim,43
Callum,22
However when I run my program I get the error message:
sortedList = sorted(reader, key=itemgetter(1), reverse=True)
IndexError: list index out of range
Edit:
I have solved this issue, the problem wasn't the sort function itself but it was a problem with the file that only worked in python 2.7 for some reason.