Python There is a serialization process called pickle, It can realize the mutual transformation between any object and text , It can also realize the mutual transformation between any object and binary . in other words ,pickle Can achieve Python Object storage and recovery .
It is worth mentioning that ,pickle yes python A standard module of language , install python At the same time, it has been installed pickle library , So it doesn't need to be installed separately , Use import Import it into the program , You can use it directly .
pickle The module provides the following 4 Functions for us to use :
Above this 4 There are two kinds of functions , among dumps and loads Implement memory based Python Object and binary translate to each other ;dump and load Implement file based Python Object and binary translate to each other .
pickle.dumps() function
This function is used to Python Object to binary object , The syntax is as follows :
dumps(obj, protocol=None, *, fix_imports=True)
The meaning of each parameter in this format is :
【 example 1】
import pickle tup1 = ('I love Python', {1,2,3}, None) # Use dumps() Function will tup1 Turn into p1 p1 = pickle.dumps(tup1) print(p1)
The output is :
b'\x80\x03X\r\x00\x00\x00I love Pythonq\x00cbuiltins\nset\nq\x01]q\x02(K\x01K\x02K\x03e\x85q\x03Rq\x04N\x87q\x05.'
pickle.loads() function
This function is used to convert binary objects to Python object , The basic format is as follows :
loads(data, *, fix_imports=True, encoding='ASCII', errors='strict')
among ,data The parameter represents the binary object to be converted , Other parameters are only for compatibility Python 2.x Version and keep , You can ignore .
【 example 2】 In case 1 On the basis of , take p1 Object deserialized to Python object .
import pickle tup1 = ('I love Python', {1,2,3}, None) p1 = pickle.dumps(tup1) # Use loads() Function will p1 Turn into Python object t2 = pickle.loads(p1) print(t2)
The running result is :
('I love Python', {1, 2, 3}, None)
Be careful , In the use of loads() Function to deserialize binary objects into Python Object time , Will automatically identify transcoding protocol , So you don't need to pass the transcoding protocol in as a parameter . also , When the number of bytes of binary object to be converted exceeds pickle Of Python Object time , Extra bytes will be ignored .
pickle.dump() function
This function is used to Python Object to binary file , Its basic syntax is :
dump (obj, file,protocol=None, *, fix mports=True)
The specific meaning of each parameter is as follows :
【 example 3】 take tup1 Tuple to binary object file .
import pickle tup1 = ('I love Python', {1,2,3}, None) # Use dumps() Function will tup1 Turn into p1 with open ("a.txt", 'wb') as f: # Open file pickle.dump(tup1, f) # use dump Function will Python Object to binary object file
After running this program , It will be in the same level directory of the program file , Generate a.txt file , But because its content is binary data , So if you open it directly, you will see the garbled code .
pickle.load() function
This function and dump() The function corresponds to , Used to convert binary object files to Python object . The basic syntax format of this function is :
load(file, *, fix_imports=True, encoding='ASCII', errors='strict')
among ,file Parameter represents the binary object file to be converted ( Must be "rb" Open the way to operate the file ), Other parameters are only for compatibility Python 2.x Version and reserved parameters , You can ignore .
【 example 4】 Will example 3 The conversion a.txt Binary object converted to Python object .
import pickle tup1 = ('I love Python', {1,2,3}, None) # Use dumps() Function will tup1 Turn into p1 with open ("a.txt", 'wb') as f: # Open file pickle.dump(tup1, f) # use dump Function will Python Object to binary object file with open ("a.txt", 'rb') as f: # Open file t3 = pickle.load(f) # Convert binary objects to Python object print(t3)
The running result is :
('I love Python', {1, 2, 3}, None)
summary
What seems powerful pickle modular , In fact, it has its own short board , namely pickle Concurrent access to persistent objects... Is not supported , In a complex system environment , Especially when reading massive data , Use pickle Will make the whole system I/O Read performance becomes a bottleneck . In this case , have access to ZODB.
ZODB It's a robust 、 Multi user and object-oriented database system , Dedicated to storage Python Object data in language , It can store and manage any complex Python object , And support transaction operation and concurrency control . also ,ZODB Also in Python Based on the serialization operation of , So use... Effectively ZODB, Must learn well first pickle.