程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python pickle module: implements persistent storage of Python objects

編輯:Python

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 :

  • dumps(): take Python The objects in are serialized into binary objects , And back to ;
  • loads(): Read the given binary object data , And convert it to Python object ;
  • dump(): take Python The objects in are serialized into binary objects , And write the file ;
  • load(): Read the specified serialized data file , And return the object .

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 :

  • obj: To convert Python object ;
  • protocol:pickle Transcoding protocol of , The value is 0、1、2、3、4, among 0、1、2 Corresponding Python Early versions ,3 and 4 It corresponds to Python 3.x Version and later . When not specified , The default is 3.
  • Other parameters : For compatibility Python 2.x Version and reserved parameters ,Python 3.x Can be ignored .

【 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 :

  • obj: To convert Python object .
  • file: Convert to the specified binary , It is required that the document must be based on "wb" Open mode for operation .
  • protocol: and dumps() Function protocol Parameters have exactly the same meaning , So there's no repetition here .
  • The other parameters : For compatibility before Python 2.x Version and reserved parameters , You can ignore .

【 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.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved