Python 如何从CSV文件中按升序排序列表?

Python 如何从CSV文件中按升序排序列表?,python,sorting,csv,Python,Sorting,Csv,这是到目前为止我的代码,我想按升序排序。我知道reverse=True将对我有所帮助,但我不知道如何在这里使用它 我试过: with open('classroom1.csv') as csvfile: readCSV = csv.reader(csvfile) for row in readCSV: name = row[0] scores = [int(c) for c in row[1:]] total = sum(score

这是到目前为止我的代码,我想按升序排序。我知道
reverse=True
将对我有所帮助,但我不知道如何在这里使用它

我试过:

with open('classroom1.csv') as csvfile:
    readCSV = csv.reader(csvfile)
    for row in readCSV:
        name = row[0]
        scores = [int(c) for c in row[1:]]
        total = sum(scores)
但它不起作用


我的列表是
[userName,score1,score2,score3]
,例如
[James,5,8,4]
如果您需要按升序排序,您不应该调用
reverse=True

如果:
classroom1.csv

srt = sorted(total, key=lambda x : x[1], reverse=True)
    print(name,srt)
main.py是:

Ali,100,100
Bob,50,100
您将收到:

data=[]
with open('classroom1.csv') as csvfile:
    readCSV = csv.reader(csvfile)
    for row in readCSV:
        name = row[0]
        scores = [int(c) for c in row[1:]]
        total=sum(scores)
        data.append((name,total))
srt=sorted(data, key=lambda x : x[1])
print srt
>>>
如果我们知道你的列表是什么样子以及应该是什么样子,那会有所帮助。所以你想要
['James',5,4,8]
-->['James',4,5,8]?@Stidgeon按照分数总和排序。如果没有答案,这是没有希望的。请不要诋毁你的问题。如果你想删除你的问题,请提醒版主注意。我尝试了这段代码,但没有结果。我将其中一行代码更改为f=open('classroom1.cvs','a+'),因为我一直收到一个错误
[('Bob',150),('Ali',200)]
Program to short by userName with high score:
Here  is my cvs file data:

userName, score1,score2,score3
James, 7,3,8
Bob, 9,5,7
Yogi, 10,4,5

import csv
output = {}
first = True
f=open("D:\work\classroom1.cvs")
for row in csv.reader(f):
    if first:
        first = False
        continue
    key = row[0]
    row.pop(0)
    val = list(set(map(int, row)))[0]
    output[key] = val

for key in sorted(output):
    print ("%s, %s" % (key, output[key]))


Output:
>>> ================================ RESTART ================================
>>> 
Bob, 9
James, 8
Yogi, 10
Program to short by high score:
Here  is my cvs file data:

userName, score1,score2,score3
James, 7,3,8
Bob, 9,5,7
Yogi, 10,4,5
Abhi, 1,2,3

import csv
import operator
output = {}
first = True
f=open("D:\work\classroom1.cvs")
for row in csv.reader(f):
    if first:
        first = False
        continue
    key = row[0]
    row.pop(0)
    val = list(set(map(int, row)))[0]
    output[key] = val

sorted_output = sorted(output.items(), key=operator.itemgetter(1))
for key,val in sorted_output:
    print ("%s, %s" % (key, val))


Output:
>>> ================================ RESTART ================================
>>> 
Abhi, 1
Bob, 9
Yogi, 9
James, 10
>>>