寻求有关跨程序会话保存复杂 Python 数据结构的最佳技术的建议。
以下是我到目前为止想到的技术列表:
- pickle/cpickle
- json
- jsonpickle
- xml
- 数据库(如 SQLite)
Pickle 是最简单、最快的技术,但我的理解是,不能保证 pickle 输出可以跨不同版本工作Python 2.x/3.x 或跨 32 位和 64 位 Python 实现。
Json 只适用于简单的数据结构。 Jsonpickle 似乎纠正了这个问题,并且似乎是为了在不同版本的 Python 上工作而编写的。
序列化为 XML 或数据库是可能的,但需要额外的工作,因为我们必须自己手动进行序列化。
谢谢你,
马尔科姆
Looking for advice on the best technique for saving complex Python data structures across program sessions.
Here's a list of techniques I've come up with so far:
- pickle/cpickle
- json
- jsonpickle
- xml
- database (like SQLite)
Pickle is the easiest and fastest technique, but my understanding is that there is no guarantee that pickle output will work across various versions of Python 2.x/3.x or across 32 and 64 bit implementations of Python.
Json only works for simple data structures. Jsonpickle seems to correct this AND seems to be written to work across different versions of Python.
Serializing to XML or to a database is possible, but represents extra effort since we would have to do the serialization ourselves manually.
Thank you,
Malcolm
发布评论
评论(4)
您对 pickles 有一个误解:它们保证可以跨 Python 版本工作。您只需选择您关心的所有 Python 版本都支持的协议版本。
您遗漏的技术是 marshal,它不能保证跨 Python 版本工作(顺便说一句,这就是 .pyc 文件的编写方式)。
You have a misconception about pickles: they are guaranteed to work across Python versions. You simply have to choose a protocol version that is supported by all the Python versions you care about.
The technique you left out is marshal, which is not guaranteed to work across Python versions (and btw, is how .pyc files are written).
您遗漏了 marshal 和 搁置 模块。
另外这个 Python 文档页面涵盖了持久性
You left out the marshal and shelve modules.
Also this python docs page covers persistence
您是否看过 PySyck 或 pyYAML?
Have you looked at PySyck or pyYAML?
您的“最佳”标准是什么?
pickle
可以处理大多数 Python 结构,也可以深度嵌套(小字印刷:
cPickle.dump(protocol=-1) 在一种情况下压缩 15M pickle / 60M sqlite,但可能会损坏。
多次出现的字符串(例如国家/地区名称)可能会占用比您预期更多的内存;
请参阅内置 intern()。
)
What are your criteria for "best" ?
pickle
can do most Python structures, deeply nested ones too(Fine print:
cPickle.dump(protocol=-1) compresses, in one case 15M pickle / 60M sqlite, but can break.
Strings that occur many times, e.g. country names, may take more memory than you expect;
see the builtin intern().
)