Python 按学生姓名对数据文件进行排序
我试图按学生姓名对数据文件进行排序,这样我就可以得到所有的学生姓名以及他们的课程代码和分数在同一行。我设法按名称顺序进行排序,但数据不会拆分为单独的行,也不会将相关的类代码或分数与每个名称放在一起 代码是:Python 按学生姓名对数据文件进行排序,python,Python,我试图按学生姓名对数据文件进行排序,这样我就可以得到所有的学生姓名以及他们的课程代码和分数在同一行。我设法按名称顺序进行排序,但数据不会拆分为单独的行,也不会将相关的类代码或分数与每个名称放在一起 代码是: import random q=0 ans=0 studentnames = [] ascending = [] classcode = input("Enter your class (1,2 or 3): ") if classcode == '1': name=input("En
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)
感谢此代码读取数据文件,按名称对记录进行排序并显示结果
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
我想如果我想在第二个字段(如score)上排序,那么在sortedbyname:Sortedbyscore=sorted(score,key=operator.itemgetter(2))之后,我会这样做。我已经尝试添加以下内容来在第二个字段上排序,但似乎不起作用:sorted(sortedbyname,key=operator.itemgetter(2),reverse=True)#按第二项排序没错。要先按分数排序,然后按名称排序,可以使用
运算符。itemgetter(2,1)
我尝试了该运算符。itemgetter(1,2)先按名称排序,然后按分数排序,但不起作用,这就是为什么我尝试以不同的方式对第二个字段排序,但没有成功…问题是分数字段是一个字符串,不是数字。我已经使用int()
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