文件相关题目,代码求改
【问题描述】文件score.txt中存储了若干学生的姓名、学号和3门考试课的成绩
要求:按总分从高到低输出学生成绩记录(按学号、姓名、三门课成绩的顺序)到文件sorted.txt
【输入形式】
文件中每行为一个学生信息,形式为:姓名,学号,成绩1,成绩2,成绩3
【输出形式】
按照总分从高到低输出学生信息到文件sorted.txt,每行一个学生的信息,输出形式为:学号,姓名,成绩1,成绩2,成绩3
【样例输入】
Zhang,2011010101,66,77,88
Li,2011010102,55,48,68
Wang,2011010103,78,87,92
Zhao,2011010104,53,34,55
Liu,2011010105,55,65,75
【样例输出】
2011010103,Wang,78,87,92
2011010101,Zhang,66,77,88
2011010105,Liu,55,65,75
2011010102,Li,55,48,68
2011010104,Zhao,53,34,55
我的
zuizhong=[]
with open('score.txt','r') as f:
forlineinf.readlines():
line=line.strip('\n')
line=line.split(',')
line,line=line,line
zuizhong.append(line)
zuizhong.sort(reverse=True, key=lambda x: sum(]))
with open('sorted.txt','r') as f:
for p in zuizhong:
f.write(i+'\n' for i in zuizhong)
怎么运行不起{:10_254:} 第一个文件打开后关闭了吗 第二个文件以可读打开的你怎么写入文件呢 最后一行的写法也不对啊,i进行迭代了怎么写入到文件中呢 zuizhong=[]
with open('score.txt','r') as f:
forlineinf.readlines():
line=line.strip('\n')
line=line.split(',')
line,line=line,line
zuizhong.append(line)
zuizhong.sort(reverse=True, key=lambda x: sum(]))
with open('sorted.txt','w') as f:
for l in zuizhong:
for p in range(len(l)):
if p!=4:
f.write(l+',')
else:
f.write(l)
f.write('\n')
终于写好垃圾代码了
喏,代码给你,最佳给我{:5_108:}
zuizhong=[]
with open('score.txt','rt') as f:
for line in f:
line = line.strip()
line = line.split(',')
line,line = line,line
zuizhong.append(line)
zuizhong.sort(reverse=True, key=lambda x: sum(]))
with open('sorted.txt','wt') as f:
for p in zuizhong:
f.write(p+'\n')
页:
[1]