如何使用腌汁来保存笛子? [英] 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?
推荐答案
尝试这样: / p>
Try this:
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屋!
查看全文