扫码关注官方订阅号
python中如何使字典在程序退出时保存现有内容?
认证高级PHP讲师
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 函数中销毁对象来保存数据。
__del__
atexit
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
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
函数中销毁对象来保存数据。