如何使用泡菜来保存字典? [英] How can I use pickle to save a dict?
本文介绍了如何使用泡菜来保存字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经查看了 Python 文档 提供的信息,但我还是有点糊涂.有人可以发布示例代码来编写一个新文件,然后使用 pickle 将字典转储到其中吗?
I have looked through the information that the Python docs give, but I'm still a little confused. Could somebody post sample code that would write a new file then use pickle to dump a dictionary into it?
推荐答案
试试这个:
import pickle
a = {'hello': 'world'}
with open('filename.pickle', 'wb') as handle:
pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open('filename.pickle', 'rb') as handle:
b = pickle.load(handle)
print a == b
这篇关于如何使用泡菜来保存字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文