[英]Recalling data from a text document on python 3
I have made this script which will save players names followed by their scores. 我制作了此脚本,该脚本将保存玩家姓名及其分数。
I am looking to recall this data back into python so it can be sorted into a table in a UI. 我希望将这些数据重新调用回python,以便可以将其分类到UI中的表中。
Im sure its a simple solution but i can only find how to save to a text document. 我肯定它是一个简单的解决方案,但我只能找到如何保存到文本文档。
players=int(input("How many payers are there? "))
with open('playerscores.txt', mode='wt', encoding='utf-8') as myfile:
for i in range (players):
username=input('Enter you username: ')
score=input('Enter your score: ')
playerinfo= [username,score, '\n']
myfile.write('\n'.join(playerinfo))
There are multiple ways to do 1. you will reopen the your playerscores.txt file read the data from it store it in buffer sort it and write it again . 有多种方法可以执行以下操作:1.您将重新打开playercores.txt文件,从中读取数据并将其存储在缓冲区中,然后对其进行排序。 2. while tacking input store it in buffer sort them and then write on txt file.
2.在添加输入时,将其存储在缓冲区中进行排序,然后写入txt文件。
players=int(input("How many payers are there? "))
with open('playerscores.txt', mode='wt', encoding='utf-8') as myfile:
playerinfo = []
for i in range (players):
username=input('Enter you username: ')
score=input('Enter your score: ')
playerinfo.append([username,score,'\n'])
playerinfo = sorted(playerinfo, key = lambda x: int(x[1]))
for i in playerinfo:
myfile.write('\n'.join(i))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.