、
#读取文件
def read_file():
result = []
with open("./student_grade_input.txt",encoding='utf-8') as fin:
for line in fin:
line = line[:-1]
result.append(line.split(","))
return result
#文件中数字排序
def sort_grades(datas):
return sorted(datas,
key=lambda x:int(x[1]),
reverse=True)
#将排好序的文件写入txt中
def write_file(datas):
with open("./student_grade_input.txt","w") as fout:
for data in datas:
fout.write(",".join(data) + "\n")
datas = read_file()
print("read_file datas:",datas)
datas = sort_grades(datas)
print("sort_grades datas:",datas)
write_file(datas)