[英]Order CSV with two digit numbers
我正在嘗試為游戲創建排行榜,在玩游戲后,python 腳本訪問 CSV 文件(未排序)並打印出得分最高的前 5 人。 Python 似乎可以很好地處理一位數字,但我無法讓它處理兩位數字。 這是代碼:
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)
這是 output:(我現在只使用占位符名稱)
['Matt Damon', ' 12']
['Robin Williams', ' 14']
['Billy Crystal', ' 15']
['Minnie Driver', ' 17']
['Peter Sellers', ' 6']
['Robert De Niro', ' 8']
['Stanley Kubrick', ' 9']
這是原始的排行榜文件:
Matt Damon, 12
Robert De Niro, 8
Billy Crystal, 15
Peter Sellers, 6
Stanley Kubrick, 9
Robin Williams, 14
Minnie Driver, 17
我如何才能正確訂購數字?
您的數據可能不會被識別為數字?
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])
給我:
[['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']
我建議只需使用 file.readlines() 讀取文件,然后處理完成的結果。
輸入文件:
Matt Damon, 12
Robert De Niro, 8
Billy Crystal, 15
Peter Sellers, 6
Stanley Kubrick, 9
Robin Williams, 14
Minnie Driver, 17
代碼:
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
要僅打印有序列表sorted_data
中的前五個分數,請使用 for x in range() 循環:
for x in range(5):
name, score = sorted_data[x]
print(f'{name} got a score of {score}')
這個 for 循環將運行 5 次。 它解壓縮存儲在sorted_data x
的元組中包含的名稱和分數,然后打印出該數據。
您號碼中的答案是通過字符串識別,而不是 integer。 嘗試使用 int() 轉換分數,但注意不要輸入 operator.itemgetter object。 int() function 只接受字符串、字節之類的 object 或數字。
sample = open('csv_sample2.txt', 'r')
csv1 = csv.reader(sample,delimiter=',')
sort = sorted(csv1, key=lambda k: int(k[1]))
如果您不想要空間,只需使用 str.strip() 擺脫它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.