#输入文件 #三列:学号、姓名、成绩 #列之间用逗号分隔,比如"101,小张,88" #行之间用\n换行分割 #处理:读取文件,按成绩倒序排列 #输入:排序后的三列数据
def read_file():
result = []
with open("012student_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[2]),reverse=True)
def write_file(datas):
with open("012student_grade_output.txt", "w", encoding="utf-8") as fout:
for data in datas:
fout.write(",".join(data) + "\n")
#1.读取文件
datas = read_file()
print("read_file datas:",datas)
#2.排序数据
datas = sort_grades(datas)
print("sort_grades datas:",sort_grades(datas))
#3.写出文件
write_file(datas)
print("write_file datas:",write_file(datas))
附件:
101,小张,88 102,小王,77 103,小李,99 104,小赵,66 105,小强,55