You also cannot write a tuple to the file which is what newrecord = score1,",",score2,",",score
is creating. You need to make a string from the scores and write that:
newrecord = "{},{},{}
".formmat(score1,score2,score3)
file.write(newrecord)
To sort existing scores you just need to call sorted on each row, using int
as the key to sorted so you values are compared as integers not strings, set reverse=True
to order from highest to lowest.
with open('room2.csv','r') as csvfile:
readCSV = csv.reader(csvfile)
for row in readCSV:
srt_row = sorted(row,key=int,reverse=True)
To sum each row and then sort, sum the values after mapping to ints again setting reverse=True
to sort from high to low:
with open('room2.csv') as csvfile:
readCSV = csv.reader(csvfile)
srt = sorted((sum(map(int,row)) for row in readCSV), reverse=True)
If you want to write to a file:
with open('room2.csv','r') as csvfile, open("out.csv","w") as out:
readCSV = csv.reader(csvfile)
for scre in sorted((sum(map(int, row)) for row in readCSV), reverse=True):
out.write(str(scre))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…