![](/img/trans.png)
[英]Python 3.6.5 How to display the top5 scores from a file with names?
[英]What is this error and how do I therefore display top 5 scores from a .csv file?
我正在写一个骰子游戏,游戏结束后,两个玩家的名字都被保存到leaderboard2.csv
文件中。 从leaderboard2.csv
,我试图显示前 5 名的分数。 这是怎么做的?
我看了很多教程,但它们都使用.txt
文件,而我有一个.csv
文件(在 Excel 中)用于排行榜。
我正在使用 python 3.3.4,我的代码就在下面:
import operator
with open("leaderboard2.csv", "r") as l:
try:
sort_key = operator.itemgetter(0)
split_lines = (line.split(None, 1) for line in l) # splits the file into it's individual lines
numeric_lines = ((int(line[0]), line[1]) for line in split_lines) # splits the numbers and the letters
score = sorted(numeric_lines, key=sort_key, reverse=True) # sorts the numbers and letters into the opposite order
leader_board1 = (score[0])
leader_board2 = (score[1])
leader_board3 = (score[2])
leader_board4 = (score[3])
leader_board5 = (score[4]) # gets the fifth line
except IndexError:
leader_board1 = '\n'
leader_board2 = '\n'
leader_board3 = '\n'
leader_board4 = '\n'
leader_board5 = '\n'
print("--------- LEADER BOARD ---------")
sys.stdout.write("FIRST PLACE: "), print(*leader_board1, sep=" points: ", end='')
sys.stdout.write("SECOND PLACE: "), print(*leader_board2, sep=" points: ", end='\n')
sys.stdout.write("THIRD PLACE: "), print(*leader_board3, sep=" points: ", end='')
sys.stdout.write("FOURTH PLACE: "), print(*leader_board4, sep=" points: ", end='')
sys.stdout.write("FIFTH PLACE: "), print(*leader_board5, sep=" points: ", end='')
input(">>> ")
这是我收到的错误:
numeric_lines = ((int(line[0]), line[1]) for line in split_lines) # splits the numbers and the letters
ValueError: invalid literal for int() with base 10: 'ok,46'
对了, ok,46
是.csv
文件的第一行( ok
是用户名, 46
是分数)
1)这个错误是什么意思,我该如何解决?
2) 我还能如何显示.csv
文件中的前 5 个分数?
我的 cvs 文件如下所示:
k,32
no,75
test,33
example,65
a,10
l,50
提前致谢。
这是使用 Python 3.6+ 及其标准模块的解决方案:
import csv
from pathlib import Path
from collections import Counter
csvfile = Path('leaderboard2.csv')
with csvfile.open() as f:
scores = Counter(dict(csv.reader(f)))
# convert scores into integers
# (there are better ways but I'm being didactic)
for name in scores:
scores[name] = int(scores[name])
# list the top five
for name, score in scores.most_common(5):
print(f'{name}: {score}')
这是一个替代版本:
import csv
from pathlib import Path
from collections import Counter
csvfile = Path('leaderboard2.csv')
scores = Counter()
with csvfile.open() as f:
for name,score in csv.reader(f):
# convert score into integer
score = int(score)
scores[name] = score
# list the top five
for name, score in scores.most_common(5):
print(f'{name}: {score}')
这是一个适用于早期版本的 Python 3 的版本:
import csv
from collections import Counter
scores = Counter()
with open('leaderboard2.csv') as f:
for name,score in csv.reader(f):
# convert score into integer
score = int(score)
scores[name] = score
# list the top five
for name, score in scores.most_common(5):
print('%s: %s' % (name, score))
我希望它有助于作为起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.