如何将文本文本中的文本放入列表并对其进行排序?

时间:2015-03-12 16:37:32

标签: python

应将分数和名称输入文本文件的测验,然后可以选择从文本文件中输出分数从最大到最小)。我需要一段代码,将文件中的文本输出到列表中,然后允许我对列表进行排序

import os
import random

x = 0

while x <1:


    teacher = input("What is your class, choose from 'A', 'B' or 'C'? ").upper() 

    if teacher == 'A':
        if os.path.exists("classA.txt") == False: 
            myfile = open("classA.txt","w")
            myfile.close()
        x = x + 1 

    elif teacher == 'B':
        if os.path.exists("classB.txt") == False:
            myfile = open("classB.txt","w")
            myfile.close()
        x = x + 1

    elif teacher == 'C':
        if os.path.exists("classC.txt") == False:
            myfile = open("classC.txt","w")
            myfile.close()
        x = x + 1

    else:
        teacher != 'A' , 'B' , 'C' 
        print("Incorrect input, choose from 'A', 'B' or 'C' ")


score = 0
name = input("Please enter your name ")

for i in range (1,11):

    no1 = random.randint(0,11)
    no2 = random.randint(0,11)    
    question = random.randint(1,3)


    if question == 1:
        answer = no1 + no2
        guess = int(input("Question "+ str(i) + ") What is " + str(no1) + "+" + str(no2) + "= "))
        if guess == answer:
            score +=1
            print("That's the correct answer")
        else:
            print ("That's the incorrect answer")

    elif question == 2:
        answer = no1 * no2
        guess = int(input("Question "+ str(i) + ") What is " + str(no1) + "*" + str(no2) + "= "))
        if guess == answer:
            score +=1
            print("That's the correct answer")          
        else:
            print ("That's the incorrect answer")


    elif question == 3:
        answer = no1 - no2
        guess = int(input("Question "+ str(i) + ") What is " + str(no1) + "-" + str(no2) + "= "))
        if guess == answer:
            score +=1
            print("That's the correct answer")

        else:
            print ("That's the incorrect answer")

print ("Your score is " + str(score))



if teacher == 'A':
    classA = []
    classA.append([name , str(score)])
    classA = open("classA.txt","a")
    classA.write(name + str(score)+"\n")
    classA.close()


elif teacher == 'B':
    classB = []
    classB.append([name , str(score)])
    classB = open("classB.txt","a")
    classB.write(name + str(score)+"\n")
    classB.close()


elif teacher == 'C':
    classC = []
    classC.append([name ,  str(score)])
    classC = open("classC.txt","a")
    classC.write(name + str(score)+"\n")
    classC.close()

4 个答案:

答案 0 :(得分:0)

仅使用内置函数:

假设您的文本文件如下所示:

John 13
Jill 12
Juan 14
Phil 15
bill 16

你会对此产生影响:

filen = open('filename.txt', 'r+')
filetxt = filen.read()
#filelist = []; filelist = filetxt.split(' ') #Splits by spaces
filelist = []; filelist = filetxt.split('\n')
#filelist = []; filelist = filetxt.split('') #Put whatever it should split by in the ('')
gradelist = []
for i in filelist:
    if i[-3:][:1] == '1': s = i[-3:]
    else: s = i[-2:]
    gradelist.append(s)
    gradelist.sort()
    b=0
    for x in gradelist:
        if x in i:
            gradelist[b] = i
        b=b+1
a=gradelist[0]
gradelist.remove(a)
gradelist.insert(len(gradelist), a)

print gradelist返回['Jill 12', 'John 13', 'Juan 14', 'Phil 15', 'bill 16']

答案 1 :(得分:0)

我建议您在文件中输入分数时,使用分隔符分隔分数和名称。例如:classA.write(name +“ - ”+ str(score)+“\ n”)

这段代码将执行Required:

import operator
f=open("classA.txt")
content={}
for line in f:
    content[line.strip().split("-")[0]]= line.strip().split("-")[1]
sorted_x = reversed(sorted(content.items(),     key=operator.itemgetter(1)))
for aTuple in sorted_x:
    print(aTuple[0] +" : "+aTuple[1])

答案 2 :(得分:0)

使用列表推导将内容读入列表列表..然后使用itemgettersort

from operator import itemgetter

lines = [line.strip().split(' ') for line in open('results.txt')]

for each in lines:
    each[1] = int(each[1])
lines.sort(key=itemgetter(1))
print lines

答案 3 :(得分:-1)

将文件中的文本输入到您需要的列表中(&#34; r +&#34;这样您就可以使用阅读线)

examplefile=open("filename.txt","r+")

然后你可以使用&#34; readlines()&#34;将文件的内容保存到列表

listofcontents=examplefile.readlines()

该文件的内容现在位于列表&#34; listofcontents&#34;由原始文本文件中的每一行分隔,允许您对其进行排序。