统计csv文件中出现最多的5个分数值及其出现次数,csv文件中的大于100的分数值、x、空都不用修改。
csv文件我的问答里
文件读写、最后输出可参考如下代码:
with open("python23成绩10-25-2021.csv","r",encoding="utf-8") as f:
#ls1 = f.readlines()
str1 = f.read()
#print(ls1[0])
#print(ls1[1].strip("\n"))
#print(ls1[2])
#print(str1)
str3 = str1.replace("\n",",")
###开始补充代码:
###结束补充代码
for i in range(5):
score,num = list4[i]
print("{}:{}".format(score,num))
输入 :无输入
输出 :
100.00:x
0.00:y
xxx3:yy1
xx4:yy2
xx5:yy3
赏金=10RMB,五天后删除.
from collections import Counter
with open(r'python23成绩10-25-2021.csv', encoding='utf-8') as f:
lineCon = f.readlines()
lineCon =[i[:len(i)-1] if '\n' in i else i for i in lineCon[1:]]
lineCon =','.join(map(lambda k: ','.join(k.split(",")[3:]), lineCon))
lineCon = lineCon.split(",")
res = dict(Counter(lineCon))
res = sorted(res.items(), key=lambda x: x[1], reverse= True)
for i in res[:5]:
print("{:>6}:{:<3}".format(*i))
CSV的格式呢,给个数据格式样本
CSV文件在我主页 有