如何在从python做的csv文件中按升序排序数字 [英] How to sort numbers in ascending order in a csv file made from python

查看:1224
本文介绍了如何在从python做的csv文件中按升序排序数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到用户必须输入的分数,然后按升序排列分数,但我很难这样做。下面是我的代码到目前为止,它不显示分数,顺序。提前提前,你的帮助是非常感激。

  classa = input(你是什么类?)

如果classa ==1:

file = open(class 1.csv a +)

如果classa ==2:

file = open(room2.csv,a +)

如果classa ==3:

file = open(class3.csv,a +)

score1 = int :))

score2 = int(输入(输入分数2结果:))

score3 = int(输入(输入分数3结果: ))

newrecord = score1,score2,,,score3

file.write(newrecord)

file.write (\\\


file.close()


import csv

import operator

with open('room2.csv','r')as csvfile:

readCSV = csv.reader(csvfile,delimiter =',')

在readCSV中:

print(row)


解决方案>

你也不能写一个元组到文件,它是 newrecord = score1,,,score2,,score 正在创建。您需要从分数中创建一个字符串,并写入:

  newrecord ={},{},{} \ n.formmat(score1,score2,score3)

file.write(newrecord)


$ b b

要排序现有的分数,只需要在每一行上调用排序,使用 int 作为排序的键,以便将值作为整数而不是字符串进行比较,设置<

 使用open(' room2.csv','r')as csvfile:
readCSV = csv.reader(csvfile)
对于readCSV中的行:
srt_row = sorted(row,key = int,reverse = True )

要对每行进行求和,然后对值进行求和,再映射到ints后设置 reverse = True 以从高到低排序:

  csv')as csvfile:
readCSV = csv.reader(csvfile)
srt = sorted(对于readCSV中的行,sum(map(int,row)),reverse = True)

如果要写入文件:

 打开('room2.csv','r')as csvfile,打开(out.csv,w)如下:
readCSV = csv.reader(csvfile )
for scre in sorted(对于readCSV中的行,是(sum(map(int,row)),reverse = True):
out.write(str(scre))


I want to get the scores which the user has to input then order the scores in ascending order however I am struggling to do so. Below is my code so far and it doesn't display the scores in order.Thanks in advance and your help is very much appreciated.

classa = input("what class are you in?")

if classa == "1":

    file=open("class 1.csv", "a+")

if classa == "2":

    file=open("room2.csv", "a+")

if classa == "3":

    file=open("class3.csv", "a+")

score1= int(input("Enter the score 1 results: "))

score2= int(input("Enter the score 2 results: "))

score3= int(input("Enter the score 3 results: "))

newrecord =score1,",",score2,",",score3

file.write(newrecord)

file.write("\n")

file.close()


import csv

import operator

with open('room2.csv','r') as csvfile:

    readCSV = csv.reader(csvfile, delimiter=',')

    for row in readCSV:

        print (row)

解决方案

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 = "{},{},{}\n".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))

这篇关于如何在从python做的csv文件中按升序排序数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆