簡體   English   中英

我試圖在 python 中做一些代碼,它讀取一個文本文件並挑選出數字最大的 5 行並打印它們

[英]Im trying to do some code in python that reads a text file and picks out the 5 lines with the highest number and prints them

我有一項任務即將完成,我必須編寫一個骰子游戲。 它的多人隨機運氣與賭博點數等

它要求的一件事是將獲勝玩家的名字和他們的分數保存到一個文本文件中,最后用五個玩家的名字在文本文件中打印五個最高分數,我得到了代碼,這樣它就可以保存玩家姓名和分數以及一些文本,但完全不知道如何閱讀整個文本文件並挑選出具有 5 個最大整數的行並打印它們

`

name = str(input("Player 1 name"))
name2 = str(input("Player 2 name"))
score = str(input("Player 1 score"))
score2 = str(input("Player 2 score"))
text_file = open("CH30.txt", "r+")
if score > score2:
    content = text_file.readlines(30)
    if len(content) > 0 :
        text_file.write("\n")
    text_file.write(name)
    text_file.write (" wins with ")
    text_file.write (score)
    text_file.write (" points")
else:
    content = text_file.readlines(30)
    if len(content) > 0 :
        text_file.write("\n")
    text_file.write (name2)
    text_file.write (" wins with ")
    text_file.write (score2)
    text_file.write (" points")


`

完整的游戲沒有附加,因為我目前在我爸爸的房子里,忘記帶我的 usb 棍子任何關於如何做到這一點的幫助將不勝感激:)

如果文件中的數據具有以下格式:

Player1 wins with 30 points
Player2 wins with 40 points
Player3 wins with 85 points
Player5 wins with 45 points
Player7 wins with 10 points
Player6 wins with 80 points
Player4 wins with 20 points
Player9 wins with 90 points
Player8 wins with 70 points
Player11 wins with 120 points
Player10 wins with 15 points

並且文件的第一行沒有空格,因為你在第一行寫了text_file.write("\n") 請在行尾添加,即最后一個文件寫入。

例如:

text_file.write(name)
text_file.write (" wins with ")
text_file.write (score)
text_file.write (" points")
text_file.write("\n")

以下是如何獲得文件的 5 個最高分數:

text_file = open('CH30.txt.', 'r')
Lines = text_file.readlines()
PlayersScores = []

# read each line get the player name and points 
for line in Lines:
    # split the line into list of strings
    line = line.split(" ")
    # removing \n from last element
    line[-1] = line[-1].replace("\n", "")
    print(line)
    # find player name position
    playerName = line.index("wins") - 1
    # find points position
    points = line.index("points") - 1
    # add the tuple (playerName, points) in a list
    PlayersScores.append((line[playerName], line[points]))
# descending order sort by player score
PlayersScores = sorted(PlayersScores, key=lambda t: t[1], reverse=True)

# get the first 5 players
print("Highest Scores:\n")
for i in range(5):
    print(str(i+1) + ". " + PlayersScores[i][0] + " " + PlayersScores[i][1] + " points")

Output 示例:

Highest Scores:

1. Player11 120 points
2. Player9 90 points
3. Player3 85 points
4. Player6 80 points
5. Player8 70 points

首先,您應該讀取文件的行列表。 如果您不知道如何執行此操作,請檢查:
如何將文件逐行讀取到列表中?

接下來,您必須對行進行排序。 我們可以使用lambda來實現。 假設單行看起來像這樣:
"Rick wins with 98 points"

排序 function 看起來像這樣:
scores.sort(key= lambda x: int(x.split(" ")[3]))

然后,您只需要對最后五個元素進行切片。

請注意,此解決方案容易出現許多錯誤,因為您使用的是人類可讀格式的文件。 對我們來說容易閱讀的內容通常很難被計算機閱讀(反之亦然)。 因此,如果您不想將該文件用作睡前讀物,請考慮使用一些更易於解析的格式 - 即 CSV

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM