pickle
- pickle 模块可以用来(解)序列化 python 对象
- pickle 功能可以将程序运行中的对象保存为文件。如果加载保存过的 pickle 文件,可以立刻复原之前程序运行中的对象。
- 官方文档地址
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
- 将对象(obj)的pickle表示写入打开的文件对象(file)中。
pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict")
- Read a pickled object representation from the open file object file and return the reconstituted object hierarchy specified therein.
Python object serialization
- The pickle module implements binary protocols for serializing and de-serializing a Python object structure.
- The data format used by pickle is Python-specific. This has the advantage that there are no restrictions imposed by external standards such as JSON or XDR (which can’t represent pointer sharing); however it means that non-Python programs may not be able to reconstruct pickled Python objects.
By default, the pickle data format uses a relatively compact binary representation. If you need optimal size characteristics, you can efficiently compress pickled data.
The module pickletools contains tools for analyzing data streams generated by pickle. pickletools source code has extensive comments about opcodes used by pickle protocols.
There are currently 5 different protocols which can be used for pickling. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.
- The pickle module can transform a complex object into a byte stream and it can transform the byte stream into an object with the same internal structure. Perhaps the most obvious thing to do with these byte streams is to write them onto a file, but it is also conceivable to send them across a network or store them in a database. The shelve module provides a simple interface to pickle and unpickle objects on DBM-style database files.
pkl文件是python里面保存文件的一种格式,如果直接打开会显示一堆序列化的东西。
3. pkl文件是什么样的?
4. 为什么要使用pkl文件
5. 怎样使用pkl文件