Python 简明教程
Python - Serialization
Serialization Libraries in Python
Python 提供了几个用于序列化的库,每个都有自己的优点。以下是 Python 中一些常用的序列化库的详细信息 −
-
Pickle − 这是 Python 内置的用于序列化和反序列化 Python 对象的模块。它易于使用,但特定于 Python,如果与不受信任的数据配合使用,可能会产生安全隐患。
-
JSON − JSON(JavaScript 对象表示法)是一种轻量级的数据交换格式,人类可读且易于解析。它非常适用于 Web API 和跨平台通信。
-
YAML − YAML:YAML(YAML Ain’t Markup Language)是一种人类可读的数据序列化标准,人类和机器都易于阅读和编写。它支持复杂的数据结构,通常用于配置文件。
Serialization Using Pickle Module
Python 中的 pickle 模块用于序列化和反序列化对象。序列化也称为 pickling ,包括将 Python 对象转换为字节流,然后可以将其存储在文件中或通过网络传输。
反序列化或 unpickling 是相反的过程,将字节流转换回 Python 对象。
Serializing an Object
我们可以使用 dump() 函数序列化对象并将其写入文件。必须以二进制写入模式 ('wb') 打开文件。
Example
在以下示例中,对字典进行序列化并写入名为 "data.pkl" 的文件 −
import pickle
data = {'name': 'Alice', 'age': 30, 'city': 'New York'}
# Open a file in binary write mode
with open('data.pkl', 'wb') as file:
# Serialize the data and write it to the file
pickle.dump(data, file)
print ("File created!!")
当执行上述代码时,字典对象的字节表示将存储在 data.pkl 文件中。
Deserializing an Object
要反序列化或取消腌制对象,可以使用 load() 函数。必须以二进制读取模式 ('rb') 打开文件,如下所示 −
import pickle
# Open the file in binary read mode
with open('data.pkl', 'rb') as file:
# Deserialize the data
data = pickle.load(file)
print(data)
这将从 "data.pkl" 中读取字节流,并将其转换回原始字典,如下所示 −
{'name': 'Alice', 'age': 30, 'city': 'New York'}
Pickle Protocols
协议是用于构建和解构到/从二进制数据的 Python 对象的约定。
pickle 模块支持不同的序列化协议,更高的协议通常提供更多功能和更好的性能。目前,pickle 模块定义了以下 6 种不同的协议:
可以通过将协议作为参数传递给 pickle.dump() 函数来指定协议。
要了解 Python 安装的最高和默认协议版本,请使用 pickle 模块中定义的以下常量:
>>> import pickle
>>> pickle.HIGHEST_PROTOCOL
5
>>> pickle.DEFAULT_PROTOCOL
4
Pickler and Unpickler Classes
Python 中的 pickle 模块还定义了 Pickler 和 Unpickler 类,用于更详细地控制序列化和反序列化过程。“Pickler”类将 pickle 数据写入文件,而“Unpickler”类从文件中读取二进制数据并重建原始 Python 对象。
Using the Pickler Class
要使用 Pickler 类序列化 Python 对象,可以按照以下步骤进行:
from pickle import Pickler
# Open a file in binary write mode
with open("data.txt", "wb") as f:
# Create a dictionary
dct = {'name': 'Ravi', 'age': 23, 'Gender': 'M', 'marks': 75}
# Create a Pickler object and write the dictionary to the file
Pickler(f).dump(dct)
print ("Success!!")
执行以上代码后,字典对象的字节表示将存储在“data.txt”文件中。
Using the Unpickler Class
要使用 Unpickler 类从二进制文件中反序列化数据,可以执行以下操作:
from pickle import Unpickler
# Open the file in binary read mode
with open("data.txt", "rb") as f:
# Create an Unpickler object and load the dictionary from the file
dct = Unpickler(f).load()
# Print the dictionary
print(dct)
我们得到了如下输出 −
{'name': 'Ravi', 'age': 23, 'Gender': 'M', 'marks': 75}
Pickling Custom Class Objects
pickle 模块还可以序列化和反序列化自定义类。类定义在腌制和反腌制的过程中都必须可用。
Example
在此示例中,一个“Person”类的实例被序列化然后反序列化,维持对象的 state:
import pickle
class Person:
def __init__(self, name, age, city):
self.name = name
self.age = age
self.city = city
# Create an instance of the Person class
person = Person('Alice', 30, 'New York')
# Serialize the person object
with open('person.pkl', 'wb') as file:
pickle.dump(person, file)
# Deserialize the person object
with open('person.pkl', 'rb') as file:
person = pickle.load(file)
print(person.name, person.age, person.city)
执行上面的代码后,我们得到以下输出: -
Alice 30 New York
Using JSON for Serialization
JSON(JavaScript 对象表示法)是一种流行的数据交换格式。它具有可读性、易写性且与语言无关,使其非常适合序列化。
Python 通过 json 模块提供对 JSON 的内置支持,该模块允许您将数据序列化和反序列化为 JSON 格式。
Serialization
序列化是将 Python 对象转换成一个 JSON 字符串或将其写入一个文件的过程。
Example: Serialize Data to a JSON String
在下例中,我们使用 json.dumps() 函数将 Python 字典转换成 JSON 字符串:
import json
# Create a dictionary
data = {"name": "Alice", "age": 25, "city": "San Francisco"}
# Serialize the dictionary to a JSON string
json_string = json.dumps(data)
print(json_string)
以下是上面代码的输出: -
{"name": "Alice", "age": 25, "city": "San Francisco"}
Example: Serialize Data and Write to a File
在此,我们使用 json.dump() 函数将序列化的 JSON 数据直接写入文件:
import json
# Create a dictionary
data = {"name": "Alice", "age": 25, "city": "San Francisco"}
# Serialize the dictionary and write it to a file
with open("data.json", "w") as f:
json.dump(data, f)
print ("Success!!")
Deserialization
反序列化是将 JSON 字符串转换回 Python 对象或从文件中读取它的过程。
Example: Deserialize a JSON String
在以下示例中,我们使用 json.loads() 函数将 JSON 字符串转换回 Python 字典:
import json
# JSON string
json_string = '{"name": "Alice", "age": 25, "city": "San Francisco"}'
# Deserialize the JSON string into a Python dictionary
loaded_data = json.loads(json_string)
print(loaded_data)
它将生成如下输出:
{'name': 'Alice', 'age': 25, 'city': 'San Francisco'}
Example: Deserialize Data from a File
在此,我们使用 json.load() 函数从文件读取 JSON 数据并将其转换成 Python 字典−
import json
# Open the file and load the JSON data into a Python dictionary
with open("data.json", "r") as f:
loaded_data = json.load(f)
print(loaded_data)
获得的输出如下 −
{'name': 'Alice', 'age': 25, 'city': 'San Francisco'}
Using YAML for Serialization
YAML(YAML Ain’t Markup Language)是一种人类可读的数据序列化标准,通常用于配置文件和数据交换。
Python 通过 pyyaml 包支持 YAML 序列化和反序列化,需要先按如下所示安装 −
pip install pyyaml
Example: Serialize Data and Write to a YAML File
在下面的示例中,yaml.dump() 函数将 Python 字典数据转换成 YAML 字符串,并将其写入文件 "data.yaml"。
"default_flow_style" 参数确保 YAML 输出更具人类可读性,并采用展开格式 −
import yaml
# Create a Python dictionary
data = {"name": "Emily", "age": 35, "city": "Seattle"}
# Serialize the dictionary and write it to a YAML file
with open("data.yaml", "w") as f:
yaml.dump(data, f, default_flow_style=False)
print("Success!!")
Example: Deserialize Data from a YAML File
在此,yaml.safe_load() 函数用于从 "data.yaml" 中安全加载 YAML 数据,并将其转换成 Python 字典 (loaded_data) −
import yaml
# Deserialize data from a YAML file
with open("data.yaml", "r") as f:
loaded_data = yaml.safe_load(f)
print(loaded_data)
下面显示了产生的输出:
{'age': 35, 'city': 'Seattle', 'name': 'Emily'}