[英]Order CSV with two digit numbers
I'm trying to create a leaderboard for a game, where after the game has been played, the python script accesses a CSV file (not ordered) and prints out the top 5 people with the highest score.我正在尝试为游戏创建排行榜,在玩游戏后,python 脚本访问 CSV 文件(未排序)并打印出得分最高的前 5 人。 Python seems to do this fine with single-digit numbers, but I can't get it to work with 2 digit numbers.
Python 似乎可以很好地处理一位数字,但我无法让它处理两位数字。 Here is the code:
这是代码:
import csv
import operator
sample = open('csv_sample2.txt', 'r')
csv1 = csv.reader(sample,delimiter=',')
sort = sorted(sample, key=operator.itemgetter(1))
for eachline in sort:
print(eachline)
and here is the output: (I just used placeholder names for now)这是 output:(我现在只使用占位符名称)
['Matt Damon', ' 12']
['Robin Williams', ' 14']
['Billy Crystal', ' 15']
['Minnie Driver', ' 17']
['Peter Sellers', ' 6']
['Robert De Niro', ' 8']
['Stanley Kubrick', ' 9']
and here is the original leaderboard file:这是原始的排行榜文件:
Matt Damon, 12
Robert De Niro, 8
Billy Crystal, 15
Peter Sellers, 6
Stanley Kubrick, 9
Robin Williams, 14
Minnie Driver, 17
How do I get it to order the numbers properly?我如何才能正确订购数字?
Your data might not be recognized as numbers?您的数据可能不会被识别为数字?
data = [['Matt Damon', ' 12'],
['Robin Williams', ' 14'],
['Billy Crystal', ' 15'],
['Minnie Driver', ' 17'],
['Peter Sellers', ' 6'],
['Robert De Niro', ' 8'],
['Stanley Kubrick', ' 9']]
# conversion to floats
data = [[item[0], float(item[1])] for item in data]
sorted(data, key=lambda x: x[1])
Gives me:给我:
[['Peter Sellers', 6.0],
['Robert De Niro', 8.0],
['Stanley Kubrick', 9.0],
['Matt Damon', 12.0],
['Robin Williams', 14.0],
['Billy Crystal', 15.0],
['Minnie Driver', 17.0]]
...: sample = [['Matt Damon', ' 12'],
...: ['Robin Williams', ' 14'],
...: ['Billy Crystal', ' 15'],
...: ['Minnie Driver', ' 17'],
...: ['Peter Sellers', ' 6'],
...: ['Robert De Niro', ' 8'],
...: ['Stanley Kubrick', ' 9']]
...:
...: def get_int_item(o):
...: return int(o[1])
...:
...:
...: sort = sorted(sample, key=get_int_item)
...:
...: for eachline in sort:
...: print(eachline)
...:
['Peter Sellers', ' 6']
['Robert De Niro', ' 8']
['Stanley Kubrick', ' 9']
['Matt Damon', ' 12']
['Robin Williams', ' 14']
['Billy Crystal', ' 15']
['Minnie Driver', ' 17']
I suggest simply reading the file with file.readlines() and then processing the finished results.我建议只需使用 file.readlines() 读取文件,然后处理完成的结果。
Input File:输入文件:
Matt Damon, 12
Robert De Niro, 8
Billy Crystal, 15
Peter Sellers, 6
Stanley Kubrick, 9
Robin Williams, 14
Minnie Driver, 17
Code:代码:
data = []
with open('your_file.txt', 'r') as f:
scores = f.readlines()
# data is a list of tuples with a string name as the
# first value and a integer score as the second value
data = [ ( s.strip().split(', ')[0], int(s.strip().split(', ')[1]) ) for s in scores ]
sorted_data = sorted(data, key=lambda tup: tup[1], reverse=True)
for name, score in sorted_data:
print(f'{name} got a score of {score}')
# Prints:
# Minnie Driver got a score of 17
# Billy Crystal got a score of 15
# Robin Williams got a score of 14
# Matt Damon got a score of 12
# Stanley Kubrick got a score of 9
# Robert De Niro got a score of 8
# Peter Sellers got a score of 6
To print out only the first five scores from the ordered list sorted_data
, use a for x in range() loop:要仅打印有序列表
sorted_data
中的前五个分数,请使用 for x in range() 循环:
for x in range(5):
name, score = sorted_data[x]
print(f'{name} got a score of {score}')
This for loop will run 5 times.这个 for 循环将运行 5 次。 It unpacks the name and score contained in the tuple stored at
sorted_data x
, and then prints out that data.它解压缩存储在
sorted_data x
的元组中包含的名称和分数,然后打印出该数据。
The answer in your number is recognize by string, instead of integer.您号码中的答案是通过字符串识别,而不是 integer。 Try to convert the score by using int() , but be carful not to input operator.itemgetter object.
尝试使用 int() 转换分数,但注意不要输入 operator.itemgetter object。 The int() function only takes string, bytes-like object or a number.
int() function 只接受字符串、字节之类的 object 或数字。
sample = open('csv_sample2.txt', 'r')
csv1 = csv.reader(sample,delimiter=',')
sort = sorted(csv1, key=lambda k: int(k[1]))
If you do not want the space just use str.strip() to get rid of them.如果您不想要空间,只需使用 str.strip() 摆脱它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.