首页 > python中如何使字典在程序退出时保存现有内容?

python中如何使字典在程序退出时保存现有内容?

python中如何使字典在程序退出时保存现有内容?


use pickle if you want serialization and deserialization fast: http://docs.python.org/2/library/pickle.html

use json if you want human readable pure text result: http://docs.python.org/2/library/json.html

use pysqlite if you want to dump large quantity of data: https://code.google.com/p/pysqlite/


标准库里有个 shelve 模块,是 pickle 和 dbm 的封装,很适合这种场景。

我这里也有自己写的模块来完成这个(pickle)或者 YAML 版。不过仅适用于 Python 3。在 Python 2 下 __del__ 时可能会出错,需要自行在 atexit 函数中销毁对象来保存数据。

【热门文章】
【热门文章】