文件schools.txt文件是高校分数排名的数据文本文件(文件局部如图所示,有标题行),每行分别是数据,数据项之间以逗号(',')分隔,编程实现按总分升序排序,输出排序后的信息。输出如图:

无法按图样输出,‘|’不能对齐
f=open("schools.txt","r+")
txt=f.readline()
for i in txt:
if i==',':
print(format("|","^3"),end='')
else:
print(i,end='')
t=f.readline()
for i in t:
if i==',':
print(format("|","^7"),end='')
else:
print(i,end=' ')
f.close()
以上为本人错误代码

img

用下面的代码应该可以对数据进行处理和排序(我没有数据进行运行一下),你增加一个循环将list中的数据输出成题目的格式就可以了

f=open("schools.txt","r+")
txts=f.readlines()
list = []
for txt in txts:
    data = txt.split(",")
    list.append(data)

list.sort(key=lambda x:x[3])
print(list)

下面是两篇博客,你可以了解一下,上面的代码都是来自这里。
https://blog.csdn.net/roytao2/article/details/53433373

https://zhuanlan.zhihu.com/p/91137035

如有帮助,望采纳


f=open("schools.txt","r+",encoding='utf-8')

t=f.readline()

for i in t:
    if i==',':
        print(format("|","^3"),end='')
    else:
        print(i,end=' ')

txts=f.readlines()


for txt in txts[::-1]:
    for i in txt:
        if i==',':
            print(format("|","^7"),end='')
        else:
            print(i,end='')

f.close()

img

这道题考点不用对齐