Sorting integers in a csv file - python -
i have csv file looks this:
tom,10 jack,10 alice,10 ben,9
i need able sort second column highest lowest. have tried following code:
import csv file = open("bestscores.csv","r") reader = csv.reader(file, delimiter = ' ') sort = sorted(reader,key=lambda x: int(x[1]), reverse=true) print(sort)
this results in following error:
valueerror: invalid literal int() base 10: 'tom,10'
how can solve this?
you using wrong delimiter , sorting wrong index. should work you:
import csv open("bestscores.csv","r") fh reader = csv.reader(fh, delimiter = ',') sort = sorted(reader, key=lambda x: int(x[1]), reverse=true) print(sort)
Comments
Post a Comment