Is there an alternative to pickle - save a dictionary (python)

半腔热情 提交于 2019-12-01 06:49:53

Pickle is not safe when transfered by a untrusted 3rd party. Local files are just fine, and if something can replace files on your filesystem then you have a different problem.

That said, if your dictionary contains nothing but string keys and the values are nothing but Python lists, numbers, strings or other dictionaries, then use JSON, via the json module.

Presuming your dictionary contains only basic data types, the normal answer is json, it's a popular, well defined language for this kind of thing.

If your dictionary contains more complex data, you will have to manually serialise it at least part of the way.

JSON is not quite Python-way because of several reasons:

  1. It can't wrap/unwrap all Python data types: there's no support for sets or tuples.
  2. Not fast enough because it needs to deal with textual data and encodings.

Try to use sPickle instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!