Python Data Persistence 简明教程

Python Data Persistence - Pickle Module

Python 中序列化和反序列化的术语分别为 pickle 和 unpickle。Python 库中的 pickle 模块使用非常特定的 Python 数据格式。因此,非 Python 应用程序可能无法正确反序列化 pickle 数据。还建议不要从未经身份验证的源中反序列化数据。

Python’s terminology for serialization and deserialization is pickling and unpickling respectively. The pickle module in Python library, uses very Python specific data format. Hence, non-Python applications may not be able to deserialize pickled data properly. It is also advised not to unpickle data from un-authenticated source.

序列化的(pickle)数据可以存储在字节字符串或二进制文件中。此模块定义了 dumps()loads() 函数,使用字节字符串对数据进行 pickle 和 unpickle 处理。对于基于文件的进程,该模块具有 dump()load() 函数。

The serialized (pickled) data can be stored in a byte string or a binary file. This module defines dumps() and loads() functions to pickle and unpickle data using byte string. For file based process, the module has dump() and load() function.

Python 的 pickle 协议是用于构建和解构 Python 对象到/从二进制数据中的约定。目前,pickle 模块定义了如下列出的 5 个不同的协议−

Python’s pickle protocols are the conventions used in constructing and deconstructing Python objects to/from binary data. Currently, pickle module defines 5 different protocols as listed below −

Sr.No.

Names & Description

1

Protocol version 0 Original “human-readable” protocol backwards compatible with earlier versions.

2

Protocol version 1 Old binary format also compatible with earlier versions of Python.

3

Protocol version 2 Introduced in Python 2.3 provides efficient pickling of new-style classes.

4

Protocol version 3 Added in Python 3.0. recommended when compatibility with other Python 3 versions is required.

5

Protocol version 4 was added in Python 3.4. It adds support for very large objects

Example

pickle 模块包含返回腌制数据的字符串表示形式的 dumps() 函数。

The pickle module consists of dumps() function that returns a string representation of pickled data.

from pickle import dump
dct={"name":"Ravi", "age":23, "Gender":"M","marks":75}
dctstring=dumps(dct)
print (dctstring)

Output

b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x04\x00\x00\x00Raviq\x02X\x03\x00\x00\x00ageq\x03K\x17X\x06\x00\x00\x00Genderq\x04X\x01\x00\x00\x00Mq\x05X\x05\x00\x00\x00marksq\x06KKu.

Example

使用 loads() 函数,可以解腌制该字符串并获取原始字典对象。

Use loads() function, to unpickle the string and obtain original dictionary object.

from pickle import load
dct=loads(dctstring)
print (dct)

Output

{'name': 'Ravi', 'age': 23, 'Gender': 'M', 'marks': 75}

还可以使用 dump() 函数将腌制对象持久存储在磁盘文件中,并使用 load() 函数检索。

Pickled objects can also be persistently stored in a disk file, using dump() function and retrieved using load() function.

import pickle
f=open("data.txt","wb")
dct={"name":"Ravi", "age":23, "Gender":"M","marks":75}
pickle.dump(dct,f)
f.close()

#to read
import pickle
f=open("data.txt","rb")
d=pickle.load(f)
print (d)
f.close()

pickle 模块还提供了面向对象 API,用于以 PicklerUnpickler 类形式表示的序列化机制。

The pickle module also provides, object oriented API for serialization mechanism in the form of Pickler and Unpickler classes.

如上所述,就像 Python 中的内置对象一样,用户定义类中的对象也可以持久序列化在磁盘文件中。在下列程序中,我们定义了一个 User 类,姓名和移动号码作为其实例属性。除 init () 构造函数外,该类还重写了 str () 方法,返回其对象的字符串表示形式。

As mentioned above, just as built-in objects in Python, objects of user defined classes can also be persistently serialized in disk file. In following program, we define a User class with name and mobile number as its instance attributes. In addition to the init() constructor, the class overrides str() method that returns a string representation of its object.

class User:
   def __init__(self,name, mob):
      self.name=name
      self.mobile=mob
   def __str__(self):
return ('Name: {} mobile: {} '. format(self.name, self.mobile))

要在文件中腌制上述类的对象,我们使用 pickler 类及其 dump() 方法。

To pickle object of above class in a file we use pickler class and its dump()method.

from pickle import Pickler
user1=User('Rajani', 'raj@gmail.com', '1234567890')
file=open('userdata','wb')
Pickler(file).dump(user1)
Pickler(file).dump(user2)
file.close()

相反,Unpickler 类有一个 load() 方法,可以如下检索已序列化的对象 −

Conversely, Unpickler class has load() method to retrieve serialized object as follows −

from pickle import Unpickler
file=open('usersdata','rb')
user1=Unpickler(file).load()
print (user1)