模块 pickle 实现了对一个 Python 对象结构的二进制序列化和反序列化。 "pickling" 是将 Python 对象及其所拥有的层次结构转化为一个字节流的过程,而 "unpickling" 是相反的操作,会将(来自一个 binary file 或者 bytes-like object 的)字节流转化回一个对象层次结构。 pickling(和 unpickling)也被称为“序列化”, “编组” [1] 或者 “平面化”。而为了避免混乱,此处采用术语 “封存 (pickling)” 和 “解封 (unpickling)”。
1. 安装
pip install pickle
2. 基本用法
- 保存数据:使用
pickle.dump()
函数将对象保存到文件中。例如:
import pickle
data = {'key': 'value'}
with open('data.pkl', 'wb') as f:pickle.dump(data, f)
- 加载数据:使用
pickle.load()
函数从文件中读取并恢复对象。例如:
with open('data.pkl', 'rb') as f:loaded_data = pickle.load(f)
print(loaded_data) # 输出: {'key': 'value'}
- 序列化自定义类:pickle可以序列化包含自定义类的对象。只需确保自定义类中没有定义
__slots__
或__getinitargs__
方法。例如:
class Person:def __init__(self, name, age):self.name = nameself.age = ageperson = Person('Alice', 30)
with open('person.pkl', 'wb') as f:pickle.dump(person, f)
3. 问题记录
3.1. 读取失败
3.1.1. 现象
import picklewith open("model_full_ckpts_0.pkl", 'rb') as f:loaded = pickle.load(f) Traceback (most recent call last):File "<stdin>", line 2, in <module>
_pickle.UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.
3.1.2. 原因
The error you're seeing happens because the pickled file you're trying to load was created using a custom class or object, and that class or object is not defined in your current code.
Here's what I would do:
Identify Custom Classes: Check the bioinformatics program's documentation or code to find out if it uses any special classes or objects when saving data.
Define Custom Classes: If you find any custom classes, create simple placeholder versions of those classes in your code.
Load Pickle File: Try loading the pickled file again after defining the necessary classes.
参考文献
玩转序列化,用这个库就对了:Python的pickle库详解-CSDN博客
pickle --- Python 对象序列化 — Python 3.13.1 文档
https://zhuanlan.zhihu.com/p/695157912
https://stackoverflow.com/questions/77603627/trouble-unpickling-a-file-pickle-unpicklingerror-a-load-persistent-id-instruct