泡菜还是json? [英] Pickle or json?

查看:120
本文介绍了泡菜还是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 ints and then recover it. Something like this:

{'juanjo': 2, 'pedro':99, 'other': 333}

什么是最佳选择,为什么?用picklesimplejson序列化它?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆