回顾python 3上的文本文档中的数据

时间:2016-09-07 12:52:32

标签: python python-3.x serialization

我制作了这个脚本,可以保存玩家名字,然后是他们的分数。

我希望将这些数据记回到python中,以便可以将其分类到UI中的表中。

我确定它是一个简单的解决方案,但我只能找到如何保存到文本文档。

    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))

1 个答案:

答案 0 :(得分:0)

有多种方法可以做 1.你将重新打开你的playerscores.txt文件,从中读取数据将其存储在缓冲区中并对其进行重新编写。 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))