Python Tutorial- Pickle Module

Python Exercise Lists

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

It is worth mentioning that pickle is a standard module of the Python language. The pickle library is already installed when Python is installed, so it does not need to be installed separately. Use import to import it into the program and use it directly.

The pickle module provides the following 4 functions for our use:

  • dumps(): serializes objects in Python into binary objects and returns them;
  • loads(): reads the given binary object data and converts it to a Python object;
  • dump(): serializes objects in Python into binary objects and writes them to a file;
  • load(): Read the specified serialized data file and return the object.

The above four functions can be divided into two categories, where dumps and loads implement memory-based Python objects and binary conversion; dump and load implement file-based Python objects and binary conversion.

Python Pickle.dumps Function

This function is used to convert a Python object to a binary object, and its syntax is as follows:

dumps (obj, protocol = None, *, fix_imports = True)

The meaning of each parameter in this format is:

obj: Python object to be converted;

protocol: Pickle's transcoding protocol. The values are 0, 1, 2, 3, and 4, where 0, 1, and 2 correspond to earlier versions of Python, and 3 and 4 correspond to Python 3.x and later versions. If not specified, the default is 3.

Other parameters: Parameters reserved for compatibility with Python 2.x versions, which can be ignored in Python 3.x.

[Example]

import pickle
tup1 = ('Python Language Website', {9,8,7}, None)
#Use the dumps () function to convert tup1 to p1
p1 = pickle.dumps (tup1)
print (p1)

The output is:

b'\x80\x03X\x17\x00\x00\x00Python Language Websiteq \x00cbuiltins\nset\nq\x01]q\x02(K\x08K\tK\x07e \x85q\x03Rq\x04N\x87q\x05.'

Pickle.loads Function

This function is used to convert a binary object into a Python object, and its basic format is as follows:

class="jumbotron1">loads (data, *, fix_imports = True, encoding = 'ASCII', errors = 'strict')

[Example] Based on Example above, deserialize the p1 object into a Python object:

import pickle (pickle_2)
tup1 = ('Python Language Website', {9,8,7}, None)
p1 = pickle.dumps (tup1)
#Use loads () to turn p1 into a Python object
t2 = pickle.loads (p1)
print (t2)

The output is:

('Python Language Website', {8, 9, 7}, None)

Note that when using the loads function to deserialize a binary object into a Python object, the transcoding protocol is automatically recognized, so there is no need to pass the transcoding protocol as a parameter. Also, when the number of bytes of the binary object to be converted exceeds the Python object of pickle, the extra bytes are ignored.

Pickle.dump Function

This function is used to convert a Python object into a binary file. Its basic syntax is:

dump (obj, file, protocol = None, *, fix mports = True)

The specific meaning of each parameter is as follows:

obj: Python object to be converted.

file: Converts to the specified binary file, which requires that the file be operated in "wb" open mode.

protocol: The meaning of the protocol parameter in the dumps() function is exactly the same, so it will not be repeated here.

Other parameters: Parameters reserved for compatibility with previous Python 2.x versions can be ignored.

[Example] Convert the tup1 tuple into a binary object file:

import pickle
tup1 = ('Python Language Website', {9,8,7}, None)
#Use the dumps () function to convert tup1 to p1
with open ("a.txt", 'wb') as f: #Open the file
    pickle.dump (tup1, f) #Use the dump function to convert Python objects to binary object files

After running this program, a.txt file will be generated in the same directory of the program file, but because its content is binary data, you will see garbled characters when you open it directly.

Pickle.load Function

This function corresponds to the dump function and is used to convert a binary object file into a Python object. The basic syntax of the function is:

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

Among them, the file parameter represents the binary object file to be converted (the file must be manipulated with "rb" open), and other parameters are only reserved for compatibility with Python 2.x versions and can be ignored.

[Example] Convert the a.txt binary file object converted in Example above to a Python object:

import pickle
tup1 = ('Python Language Website', {9,8,7}, None)
#Use the dumps () function to convert tup1 to p1
with open ("a.txt", 'wb') as f: #Open the file
    pickle.dump (tup1, f) #Use the dump function to convert Python objects to binary object files
with open ("a.txt", 'rb') as f: #Open the file
    t3 = pickle.load (f) #Convert binary file object into Python object
    print (t3)

The output is:

('Python Language Website', {9,8,7}, None)

Conclusion

The seemingly powerful pickle module actually has its cons. That is, pickle does not support concurrent access to persistent objects. In complex system environments, especially when reading large amounts of data, using pickle will make the entire system I / O read performance becomes a bottleneck. In this case, you can use ZODB.

ZODB is a robust, multi-user, and object-oriented database system designed to store object data in the Python language. It can store and manage arbitrary complex Python objects and supports transaction operations and concurrency control. Moreover, ZODB is also implemented based on Python's serialization operation, so to effectively use ZODB, you must first learn pickle.

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises