pickle library for python

pickle library

Introduction to serialization and deserialization

Serialization: The process of converting an object into a sequence of bytes is called serialization of an object.

Deserialization: The process of restoring a sequence of bytes to an object is called deserialization of an object.

Introduction to the pickle library

There is a serialization process in Python called pickle, which can realize the mutual conversion between any object and text, and can also realize the mutual conversion between any object and binary. In other words, pickle can realize the storage and recovery of Python objects.

The pickle module can only be used in python, and almost all data types (lists, dictionaries, collections, classes, etc.) in python can be serialized with pickle

Four methods of the pickle module

dumps and loads implement memory-based conversion between Python objects and binary; dump and load implement file-based conversion between Python objects and binary

dumps()

dumps(): Serialize objects in Python into binary objects and return

dumps() method introduction

# dumps(obj, protocol=None)

obj:要转换的 Python 对象
protocol:pickle 的转码协议,取值为 0、1、2、3、4,其中 0、1、2 对应 Python 早期的版本,3 和 4 则对应 Python 3.x 版本及之后的版本。未指定情况下,默认为 3

dumps() example

import pickle

dic = {
    
    "name": "lihua", "age": 30}
pe = pickle.dumps(dic)  # 将任意数据类型类型转换成二进制对象
print(pe)
# b'\x80\x04\x95\x1c\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x04name\x94\x8c\x05lihua\x94\x8c\x03age\x94K\x1eu.'

loads()

loads(): reads the given binary object data and converts it to a Python object

Introduction to the loads() method

# loads(data)

data 参数表示要转换的二进制对象

loads() example

import pickle

dic = {
    
    "name": "lihua", "age": 30}
pe = pickle.dumps(dic)  # 将任意数据类型转换成二进制文件
dic1 = pickle.loads(pe)  # 将二进制文件恢复成原来的数据类型
print(dic1)
# {'name': 'lihua', 'age': 30}

dump()

dump(): Serialize objects in Python into binary objects and write them to files

dump() method introduction

# dump (obj, file,protocol=None)

obj:要转换的 Python 对象
file:转换到指定的二进制文件中,要求该文件必须是以"wb"的打开方式进行操作
protocol:pickle 的转码协议,取值为 0、1、2、3、4,其中 0、1、2 对应 Python 早期的版本,3 和 4 则对应 Python 3.x 版本及之后的版本。未指定情况下,默认为 3

dump() example

import pickle

dic = {
    
    "name": "lihua", "age": 30}
with open("pickle.pickle", "wb") as f:
    pickle.dump(dic, f)  # 将转换后的二进制文件存储在pickle.pickle文件里面

load()

load(): read the specified serialized data file and return the object

Introduction to the load() method

# load(file)

file 参数表示要转换的二进制对象文件(必须以 “rb” 的打开方式操作文件)

load() example

import pickle

dic = {
    
    "name": "lihua", "age": 30}
with open("pickle.pickle", "rb") as f:
    dic1 = pickle.load(f)  # 将存储的文件内容导出为python对象

print(dic1)
# {'name': 'lihua', 'age': 30}

reference:

1. Pickle library in Python

2. [pickle] Detailed explanation of the pickle module in python (commonly used functions, examples)

3. Python pickle module learning (super detailed)

This is a Link Markdown syntax .

Guess you like

Origin blog.csdn.net/qq_44659804/article/details/128837969