python对象序列号pickle

做个笔记,平时很少用,猛地一下子还忘了给...

官方示例:

写文件

python 复制代码
import pickle

# An arbitrary collection of objects supported by pickle.
data = {
    'a': [1, 2.0, 3+4j],
    'b': ("character string", b"byte string"),
    'c': {None, True, False}
}

with open('data.pickle', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

读文件

python 复制代码
import pickle

with open('data.pickle', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    data = pickle.load(f)

详见:

https://docs.python.org/zh-cn/3/library/pickle.html#example

相关推荐
狐凄15 分钟前
Python实例题:Python计算线性代数
开发语言·python·线性代数
西猫雷婶16 分钟前
pytorch基本运算-导数和f-string
人工智能·pytorch·python
述雾学java19 分钟前
深入理解 transforms.Normalize():PyTorch 图像预处理中的关键一步
人工智能·pytorch·python
要努力啊啊啊22 分钟前
使用 Python + SQLAlchemy 创建知识库数据库(SQLite)—— 构建本地知识库系统的基础《一》
数据库·人工智能·python·深度学习·自然语言处理·sqlite
Andrew_Xzw1 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
凤头百灵鸟2 小时前
Python语法基础篇(包含类型转换、拷贝、可变对象/不可变对象,函数,拆包,异常,模块,闭包,装饰器)
python
多多*3 小时前
LUA+Reids实现库存秒杀预扣减 记录流水 以及自己的思考
linux·开发语言·redis·python·bootstrap·lua
何双新4 小时前
第21讲、Odoo 18 配置机制详解
linux·python·开源
Wish3D4 小时前
阿里云OSS 上传文件 Python版本
开发语言·python·阿里云
阿福不是狗6 小时前
Python使用总结之Mac安装docker并配置wechaty
python·macos·docker