泡菜还是json? [英] Pickle or json?
问题描述
我需要将一个密钥为str
类型且值为int
s 的dict
对象保存到磁盘上,然后将其恢复.像这样:
I need to save to disk a little dict
object whose keys are of the type str
and values are int
s and then recover it. Something like this:
{'juanjo': 2, 'pedro':99, 'other': 333}
什么是最佳选择,为什么?用pickle
或simplejson
序列化它?
What is the best option and why? Serialize it with pickle
or with simplejson
?
我正在使用Python 2.6.
I am using Python 2.6.
推荐答案
如果您没有任何互操作性要求(例如,您将要在Python中使用数据)并且二进制格式也可以,请使用 cPickle ,它可以使您真正快速地进行Python对象序列化.
If you do not have any interoperability requirements (e.g. you are just going to use the data with Python) and a binary format is fine, go with cPickle which gives you really fast Python object serialization.
如果您希望互操作性或想要一种文本格式来存储数据,请使用JSON(或其他一些适当的格式,具体取决于您的约束).
If you want interoperability or you want a text format to store your data, go with JSON (or some other appropriate format depending on your constraints).
这篇关于泡菜还是json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!