我正在尝试按学生姓名对数据文件进行排序,以便我将所有学生姓名及其类别代码和分数放在同一行。我设法按名称顺序排序,但数据不会拆分为单独的行,也不会将相关的类代码或分数与每个名称放在一起。
代码是:
import random
q=0
ans=0
studentnames = []
ascending = []
classcode = input("Enter your class (1,2 or 3): ")
if classcode == '1':
name=input("Enter your name: ")
n= int(input("Enter the number of questions you want to attempt: "))
#writing details to a txt file group1
text_file1 = open("data.dat", "a")
while q<n:
c = random.randint(1,3)
print("c is: ", c)
if c==1:
f="+"
num1 = random.randint(0,12)
print("num1 is: ", num1)
num2 = random.randint(0,10)
print("num2 is: ", num2)
product=num1*num2
difference=num1-num2
total=num1+num2
d=total
q+=1
print("\n Question ", q)
print ("What is ",num1,f,num2," ? ")
l=int(input("Enter Your Answer: "))
if l==d:
print ("\nCongratulations! your answer is right.\n")
ans+=1
else:
print("\n I'm afraid your answer is wrong. The correct one is" ,d)
elif c==2:
f="*"
num1 = random.randint(0,12)
print("num1 is: ", num1)
num2 = random.randint(0,10)
print("num2 is: ", num2)
product=num1*num2
difference=num1-num2
total=num1+num2
d=product
q+=1
print("\n Question ", q)
print ("What is ",num1,f,num2," ? ")
l=int(input("Enter Your Answer: "))
if l==d:
print ("\nCongratulations! your answer is right.\n")
ans+=1
else:
print("\n I'm afraid your answer is wrong. The correct one is" ,d)
else:
f="-"
num1 = random.randint(0,12)
print("num1 is: ", num1)
num2 = random.randint(0,10)
print("num2 is: ", num2)
product=num1*num2
difference=num1-num2
total=num1+num2
d=difference
q+=1 # counter for questions
print("\n Question ", q)
print ("What is ",num1,f,num2," ? ")
l=int(input("Enter Your Answer: "))
if l==d:
print ("\nCongratulations! your answer is right.\n")
ans+=1 # counter for answers
else:
print("\n I'm afraid your answer is wrong. The correct one is" ,d)
# calculating the percentage
x=float(ans)
y=float(q)
z=ans*100/q
cp=int(z)
print("Your Correct percentage = ",cp,"%\n")
#store details in a list
scores=[classcode,name,cp]
print(scores)
#writing to text file
text_file1.write(classcode )
text_file1.write(' ')
text_file1.write(name )
text_file1.write(' ')
#converting score to string so that it can be written to txt file
scr = str(cp)
text_file1.write(scr )
#adding a new line after each students score
text_file1.write("\n")
#close the txt file after it has been used
text_file1.close()
#reading the txt file
text_file1 = open("data.dat", "r")
print(text_file1.read())
text_file1.close()
f = open("data.dat", "r")
for line in f:
fields = line.split()
classcode = fields[0]
name = fields[1]
scr = str(fields[2])
studentnames.append(classcode)
studentnames.append(name)
studentnames.append(scr)
print(classcode, name, scr)
f = open("data.dat", "r")
for line in f:
fields = line.split()
classcode = fields[0]
name = fields[1]
scr = str(fields[2])
studentnames.sort()
ascending.append(studentnames)
print(ascending)
我尝试按名称顺序排序的数据文件是:
1 test2 100
1 test1 100
1 test3 20
1 test4 60
1 test5 33
1 aguero 100
1 aguero 100
1 aguero 100
1 aguero 50
1 test3 60
1 1 100
1 test3 50
1 test2 100
1 test1 100
1 test4 50
1 messi 100
1 messi 100
1 ronaldo 100
1 ronaldo 100
1 maradonna 100
1 maradonna 100
1 maradonna 100
由于
答案 0 :(得分:0)
此代码读取数据文件,按名称对记录进行排序并显示结果。
import operator
with open("data.dat") as f:
students = []
for line in f:
fields = line.split()
students.append( (fields[0], fields[1], int(fields[2])) )
# sort by the student's name
sortedByName = sorted(students, key=operator.itemgetter(1))
for r in sortedByName:
classcode, name, score = r
print "name:", name, "classcode:", classcode, "score:", score