如何在 python 中为文本文件添加行数限制
How to add number of lines limit to a text file in python
我目前正在做一个 python 项目作为我教学大纲的一部分。其中一项标准是最多应有 20 名玩家。我该如何设置这个限制?我知道如何将它们设置为 python 内的普通变量,但不是在外部文本文件中。这可以通过限制用户输入播放器的次数来实现吗?我添加了注释,以便您了解每条语句的作用。如果需要,在何处添加代码。
def playerMenu():
runningplayer = True
while runningplayer == True:
time.sleep(0.3)
print("\n====================================================")
print("************|Welcome to The Player Menu|************")
print("====================================================")
time.sleep(0.2)
choice = input('''
====================================================
A: Add Player & Score
B: Delete Player
C: View Scores
D: Back To Main Menu
E: Exit Menu
====================================================
\nPlease select what you wish to do: ''')
# This ELIF statement will allow the user to write the name and score of the player.
if choice == "A" or choice == "a":
save_name = input('Enter your name: ').title()
save_score = input('Enter your score: ')
text_file = open("highscores.txt", "a")
text_file.write("\n" + save_name + ' | ' + save_score + "\n")
text_file.close()
text_file = open("highscores.txt", "r")
whole_thing = text_file.read()
print (whole_thing)
text_file.close()
# This ELIF statement will allow the user to delete a player and their score from the text file.
elif choice == "B" or choice == "b":
print("These are the current players and their score:")
text_file = open("highscores.txt", "r")
whole_thing = text_file.read()
print(whole_thing)
text_file.close()
time.sleep(0.3)
save_delete = input("Please enter the name of the player you wish to delete: ") + " | "
#print(f"save_delete = {save_delete}")
with open("highscores.txt", "r") as f:
lines = f.readlines()
#print(lines)
with open("highscores.txt", "w") as f:
for line in lines:
if not(line.startswith(save_delete)):
f.write(line)
# This ELIF statement will allow the user to view the scores of all players.
elif choice == "C" or choice == "c":
with open('highscores.txt', encoding = 'utf-8', mode = 'r') as my_file:
file = open("highscores.txt", encoding='utf-8', mode='r')
text = file.read()
print(text)
有多种方法可以解决这个问题,但是,我想您应该将高分 table 视为具有固定最大长度的队列。每次输入新的高分时,您都会计算所有现有的高分条目 - 如果计数超过 20,则删除最后一个(最旧的)条目。
编辑:如果您应该保存前 20 个分数,您可以在每次添加新分数时对它们进行排序,然后删除最后一个(最小的)分数。
因此,在添加播放器之前,您可以从文件中读取行。如果行数为20,则不添加播放器,如果行数较少,则可以添加。
一个简单的方法是使用 readlines()
这将 return 一个包含每一行的列表。然后你可以检查列表的长度。
# This ELIF statement will allow the user to write the name and score of the player.
save_name = input('Enter your name: ')
save_score = input('Enter your score: ')
text_file = open("untitled.txt", "r")
whole_thing = text_file.readlines()
text_file.close()
if len(whole_thing) < 20:
text_file = open("untitled.txt", "a")
text_file.write("\n" + save_name + ' | ' + save_score + "\n")
text_file.close()
text_file = open("untitled.txt", "r")
whole_thing = text_file.readlines()
print (whole_thing)
text_file.close()
我目前正在做一个 python 项目作为我教学大纲的一部分。其中一项标准是最多应有 20 名玩家。我该如何设置这个限制?我知道如何将它们设置为 python 内的普通变量,但不是在外部文本文件中。这可以通过限制用户输入播放器的次数来实现吗?我添加了注释,以便您了解每条语句的作用。如果需要,在何处添加代码。
def playerMenu():
runningplayer = True
while runningplayer == True:
time.sleep(0.3)
print("\n====================================================")
print("************|Welcome to The Player Menu|************")
print("====================================================")
time.sleep(0.2)
choice = input('''
====================================================
A: Add Player & Score
B: Delete Player
C: View Scores
D: Back To Main Menu
E: Exit Menu
====================================================
\nPlease select what you wish to do: ''')
# This ELIF statement will allow the user to write the name and score of the player.
if choice == "A" or choice == "a":
save_name = input('Enter your name: ').title()
save_score = input('Enter your score: ')
text_file = open("highscores.txt", "a")
text_file.write("\n" + save_name + ' | ' + save_score + "\n")
text_file.close()
text_file = open("highscores.txt", "r")
whole_thing = text_file.read()
print (whole_thing)
text_file.close()
# This ELIF statement will allow the user to delete a player and their score from the text file.
elif choice == "B" or choice == "b":
print("These are the current players and their score:")
text_file = open("highscores.txt", "r")
whole_thing = text_file.read()
print(whole_thing)
text_file.close()
time.sleep(0.3)
save_delete = input("Please enter the name of the player you wish to delete: ") + " | "
#print(f"save_delete = {save_delete}")
with open("highscores.txt", "r") as f:
lines = f.readlines()
#print(lines)
with open("highscores.txt", "w") as f:
for line in lines:
if not(line.startswith(save_delete)):
f.write(line)
# This ELIF statement will allow the user to view the scores of all players.
elif choice == "C" or choice == "c":
with open('highscores.txt', encoding = 'utf-8', mode = 'r') as my_file:
file = open("highscores.txt", encoding='utf-8', mode='r')
text = file.read()
print(text)
有多种方法可以解决这个问题,但是,我想您应该将高分 table 视为具有固定最大长度的队列。每次输入新的高分时,您都会计算所有现有的高分条目 - 如果计数超过 20,则删除最后一个(最旧的)条目。
编辑:如果您应该保存前 20 个分数,您可以在每次添加新分数时对它们进行排序,然后删除最后一个(最小的)分数。
因此,在添加播放器之前,您可以从文件中读取行。如果行数为20,则不添加播放器,如果行数较少,则可以添加。
一个简单的方法是使用 readlines()
这将 return 一个包含每一行的列表。然后你可以检查列表的长度。
# This ELIF statement will allow the user to write the name and score of the player.
save_name = input('Enter your name: ')
save_score = input('Enter your score: ')
text_file = open("untitled.txt", "r")
whole_thing = text_file.readlines()
text_file.close()
if len(whole_thing) < 20:
text_file = open("untitled.txt", "a")
text_file.write("\n" + save_name + ' | ' + save_score + "\n")
text_file.close()
text_file = open("untitled.txt", "r")
whole_thing = text_file.readlines()
print (whole_thing)
text_file.close()