pickle of Python standard library

1. Introduction to pickle standard library

  • pickle, as a noun, means pickles, and as a verb, means to preserve food in vinegar or brine. From this, it is not difficult to think of using storage devices to persist data. The pickle standard library is just a core library for binary serialization and deserialization of Python object structures. It is dedicated to representing a large number of data types in the Python language and is the first choice when serializing Python objects.
  • The pickle standard library is a standard version implemented in pure Python, and the cPickle standard library (renamed to _pickle in Python 3.x) is an accelerated version implemented in C language, with higher read and write efficiency.
  • In the process of actually using Python3.x, we can directly import the standard version, and the last part of the source code will try to import the corresponding function of the accelerated version instead of importing the pickle module separately, as shown below:
# Use the faster _pickle if possible
try:
    from _pickle import (
        PickleError,
        PicklingError,
        UnpicklingError,
        Pickler,
        Unpickler,
        dump,
        dumps,
        load,
        loads
    )
except ImportError:
    Pickler, Unpickler = _Pickler, _Unpickler
    dump, dumps, load, loads = _dump, _dumps, _load, _loads

1.1 Preliminary knowledge

  • Data Persistence (Data Persistence): A general term for converting an in-memory data model to a storage model, and converting a storage model to an in-memory data model, generally referring to converting transient data (such as objects in memory) into persistent data Save permanently to a storage device (such as a disk).

  • Serialization\deserialization classification: binary sequence, text sequence

  • Serialization: The process of converting the state information of an object into a form that can be stored or transmitted, with the purpose of making the state information in memory persistent for later recovery,

    It is convenient for network transmission to realize cross-platform data interaction.

  • Deserialization (Deserialization): the process of restoring a sequence into an object, which is the inverse process of serialization

1.2 Comparison between pickle and json

Compare Dimensions\Standard Library pickle json
serialization method binary sequence text sequence
human readable no yes
Cross-platform, language, protocol No, only for Python yes
The data type indicates support Can represent a large number of Python data types Can only represent a subset of Python's built-in types, not custom classes

2. Pickle common functions

2.1 pickle.dump(obj, file, protocol=None, *, fix_imports=True)

write a Python object to an opened binary file

# define a dict object
obj={
    
    
    "name":"Bonnie",
    "isAGirl":True,
    "age":22,
    "hobbies":["guitar","singing","traveling"]
}
# 将obj写入已打开的二进制文件,序列化
with open("puppy_love.pkl","wb") as f:
    pickle.dump(obj,f,protocol=pickle.HIGHEST_PROTOCOL) # 设置为可用的最高协议

insert image description here

2.2 pickle.load(file, *, fix_imports=True, encoding='ASCII', errors='strict')

Reads packed objects from an opened binary file, rebuilds the hierarchy of specific objects within it, and returns

# 从打开的二进制文件重建对象,反序列化
with open("puppy_love.pkl","rb") as f:
    obj_again=pickle.load(f)
print("反序列化后的对象为{}".format(obj_again))

insert image description here

2.3 pickle.dumps(obj, protocol=None, *, fix_imports=True)

Return the packaged object of obj directly as a bytes type instead of writing it to a file

# 将Python对象直接转为bytes类型,不写入文件
bytes_obj=pickle.dumps(obj,protocol=pickle.HIGHEST_PROTOCOL) # 设置为可用的最高协议
print("obj转换为bytes类型后为{}".format(bytes_obj))

insert image description here

2.4 pickle.loads(data, *, fix_imports=True, encoding='ASCII', errors='strict')

Reconstruct and return the object hierarchy of an object's archived representation data, the bytes type will be deserialized into a Python object

# bytes类型将反序列化为Python对象
obj_origin=pickle.loads(bytes_obj)
print("bytes类型将反序列化为obj后{}".format(obj_origin))

insert image description here

3. References

Guess you like

Origin blog.csdn.net/m0_46223009/article/details/128086147