Python - 如何使用列表对csv文件进行排序

时间:2015-07-22 09:23:47

标签: python file csv

我已将数据放在用户已完成的测验的单独文件中(顺便提一下,用户从三个中选择一个类)。现在我想按照alphbetical,最高分和每个班级的平均分数对文件中的数据进行排序。

if MyClass == "1":
    myFile = open('Class1.csv', 'a')
    myFile.write("Class = ")
    myFile.write(MyClass + " ")
    myFile.write("Name = ")
    myFile.write(name + " ")
    myFile.write("Score = ")
    myFile.write(str(score) + " ")
    myFile.write("\n")
    myFile.close()
elif MyClass == "2":
    myFile = open('Class2.csv', 'a')
    myFile.write("Class = ")
    myFile.write(MyClass + " ")
    myFile.write("Name = ")
    myFile.write(name + " ")
    myFile.write("Score = ")
    myFile.write(str(score) + " ")
    myFile.write("\n")
    myFile.close()
elif MyClass == "3":
    myFile = open('Class3.csv', 'a')
    myFile.write("Class = ")
    myFile.write(MyClass + " ")
    myFile.write("Name = ")
    myFile.write(name + " ")
    myFile.write("Score = ")
    myFile.write(str(score) + " ")
    myFile.write("\n")
    myFile.close()

此代码用于将类保存到已决定的文件中,下面的代码用于确定它们所在的类

def classes():
    #here its asking the question to the user
    theClass = input ("What class are you in: 1,2,3 ")

    # Test the input is between 1 and 3 
    while (int(theClass) > 3):
        theClass = input ("Sorry, please enter a number between 1 and 3 ")

0 个答案:

没有答案