Python 的 pickle 库使用笔记

Python 的 pickle 库使用笔记
模块 pickle 实现了对一个 Python 对象结构的二进制序列化和反序列化。pickling 是将 Python 对象及其所拥有的层次结构转化为一个字节流的过程而 unpickling 是相反的操作会将来自一个 binary file 或者 bytes-like object 的字节流转化回一个对象层次结构。pickling和 unpickling也被称为“序列化”“编组” [1] 或者 “平面化”。而为了避免混乱此处采用术语 “封存 (pickling)” 和 “解封 (unpickling)”。1. 安装pip install pickle2. 基本用法保存数据‌使用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 name self.age age person Person(Alice, 30) with open(person.pkl, wb) as f: pickle.dump(person, f)3. 问题记录3.1. 读取失败3.1.1. 现象import pickle with 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 youre seeing happens because the pickled file youre trying to load was created using a custom class or object, and that class or object is not defined in your current code.Heres what I would do:Identify Custom Classes: Check the bioinformatics programs 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/695157912https://stackoverflow.com/questions/77603627/trouble-unpickling-a-file-pickle-unpicklingerror-a-load-persistent-id-instruct