1

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.

6
  • Possible duplicate of Sorting by Specific Column data using .csv in python Commented Feb 6, 2017 at 14:42
  • 1
    This works for me (python2.7). Commented Feb 6, 2017 at 14:50
  • @Scott Hunter I'm on Python 3 and it isn't working Commented Feb 6, 2017 at 14:56
  • Use pandas! Commented Feb 6, 2017 at 15:05
  • It works fine on python3.5. Is it possible that the input file has an empty line or maybe missing scores on a line? Try an input file with just the two lines of input as you listed above. Does that work? Commented Feb 6, 2017 at 15:05

2 Answers 2

1

You can use lambda which will allow you to do type transform etc. Similar to your example above, following lines will do what you want (I have kept sort etc default):

r = csv.reader(open("test.txt"))
sorted(r, key=lambda x: int(x[1]))

You can read more details at https://wiki.python.org/moin/HowTo/Sorting

These are full details showing version and platform:

Python 2.7.10 (default, Jul 30 2016, 18:31:42) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> r = csv.reader(open("test.txt"))
>>> sorted(r, key=lambda x: int(x[1]))
[['Can', ' 2'], ['Try', ' 5'], ['Science', ' 12'], ['Math', ' 22'], ['Life', ' 35']]

where test.txt is as below:

Science,12
Math,22
Life,35
Can,2
Try,5
Sign up to request clarification or add additional context in comments.

3 Comments

See added details above. Can you just write content on screen as: > for line in reader: print line
The problem is due to python 3 I think, just tried on python 2.7.12 and both methods are now working.
That is good to know but not useful. CSV reader and sorted both should behave the same unless default arguments have changed. Did you try to write out each line read by reader on console, as suggested above? It will be good to confirm what change is failing for you.
0

My assumption is that you are missing some data. Here is my workaround for that case:

import csv

with open("highscores.txt") as data:
    data = csv.reader(data)
    data = [item for item in data]
    for i, item in enumerate(data):
        try:
            item = int(item[1])
        except:
            data[i].append('0')
    items = sorted(data, key=lambda x: int(x[1]), reverse=True)
    print(items)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.