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:
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.
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.'
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.
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.
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)More Python Exercises:
Python String Exercises