Python 腌制高分,然后打印
我想把高分整理出来,然后打印出来 在实际的程序中,分数是从一个简单的琐事游戏中获得的Python 腌制高分,然后打印,python,python-3.x,pickle,Python,Python 3.x,Pickle,我想把高分整理出来,然后打印出来 在实际的程序中,分数是从一个简单的琐事游戏中获得的 score = 10 name = input("Name: ") scores = [name, score] high_scores = open("high_scores.dat", "ab") pickle.dump(scores, high_scores) high_scores.close() high_scoresR = open("high_scores.dat", "rb") results
score = 10
name = input("Name: ")
scores = [name, score]
high_scores = open("high_scores.dat", "ab")
pickle.dump(scores, high_scores)
high_scores.close()
high_scoresR = open("high_scores.dat", "rb")
results = pickle.load(high_scoresR)
print(results)
high_scores.close()
该程序只打印输入的第一个高分,无论我尝试将多少分数转储到它。例如:
['Jason', 10]
我猜我不太懂一些基本的东西,所以我非常希望能有一个信息丰富且清晰的解释 您可以将文件读入字典:
name = input('Enter name: ')
score = input('Enter score: ')
# write new entry to file
with open('highscores.txt', 'a') as f:
f.write(name + ':' + score + '\n')
# read file into dict
with open('highscores.txt', 'r') as f:
lines = f.read().splitlines()
scores = dict(line.split(':') for line in lines)
for key, value in scores.items():
print(key, value)
我不知道您正在尝试学习
pickle
,但这可能会对其他人有所帮助。您可以使用'wb'
模式将多个pickle写入一个文件,如果您需要重新打开它以进行一个或多个转储
,那么您应该使用附加模式('a'
,而不是'w'
)。在这里,我使用'wb'
编写多个条目,然后使用'ab'
添加一个条目
>>> scores = dict(Travis=100, Polly=125, Guido=69)
>>> import pickle
>>> with open('scores.pkl', 'wb') as highscores:
... for name,score in scores.items():
... pickle.dump((name,score)), highscores)
...
>>> with open('scores.pkl', 'ab') as highscores:
... pickle.dump(scores, highscores)
...
>>> with open('scores.pkl', 'rb') as highscores:
... a = pickle.load(highscores)
... b = pickle.load(highscores)
... c = pickle.load(highscores)
... d = pickle.load(highscores)
...
>>> a
('Travis', 100)
>>> b
('Polly', 125)
>>> c
('Guido', 69)
>>> d
{'Polly': 125, 'Travis': 100, 'Guido': 69}
>>>
如果你有很多数据,以至于你担心能同时转储和/或加载你所有的项目,那么你可以使用(我的一个软件包)klepot
,它使你能够将大量的数据存储到一个文件、目录或数据库中……你可以一次无缝地访问一个条目
>>> import klepto
>>> store = klepto.archives.dir_archive('high', serialized=True)
>>> store.update(scores)
>>> store
dir_archive('high', {'Polly': 125, 'Guido': 69, 'Travis': 100}, cached=True)
>>> # dump all entries at once
>>> store.dump()
>>> # create a new empty archive proxy
>>> store2 = klepto.archives.dir_archive('high', serialized=True)
>>> store2
dir_archive('high', {}, cached=True)
>>> # load one entry, as opposed to loading all entries
>>> store2.load('Guido')
>>> store2
dir_archive('high', {'Guido': 69}, cached=True)
>>> store2['Guido']
69
>>> # load everything else
>>> store2.load()
>>> store2
dir_archive('high', {'Polly': 125, 'Guido': 69, 'Travis': 100}, cached=True)
>>>
虽然这不是你手上问题的答案,但我建议你以后使用。这样做很酷,可能会让你的生活更轻松。如果你将转储到文件中5次,那么你需要执行加载
5次。一次转储一次加载
。或者您可以在dict中获得高分,然后转储
该dict。然后,你只需要一个加载
。我必须同意其他的评论,你是想学习pickle
还是对存储数据更感兴趣?我想学习pickle,我不能一次转储整个字典/列表。谢谢,但我已经为纯文本文件编写了一个类似的程序。现在我想用pickle文件创建一个,来学习和练习这个主题。非常感谢,但是(关于第一个代码,没有klepot)如何打印整个文件?我不知道我要打印的密钥的名称。@GalA:如果您将所有分数作为dict
(或列表
,或其他单个对象)保存,您可以使用单个转储将所有内容转储到文件中。文件模式'wb'
将所有文件视为新文件(销毁旧条目),而'ab'
将始终追加。如果您的意思是要“打印
”整个文件,则可以将其视为文本文件,然后将文件作为字符串打印。但是,如果您只是想转储
所有内容,那么使用单个转储
和'wb'
——注意,在这种情况下,您必须在加载时读取单个对象中的所有分数。如果由于某种原因不能一次转储所有键,并且不知道键的名称,那么可以迭代所有条目。我会为那个案子编辑的。