python封装函数、实现将任意的对象序列化到磁盘上_序列化(serialization)
序列化是指將"結(jié)構(gòu)化數(shù)據(jù)"轉(zhuǎn)換成"可存儲(chǔ)格式"的過(guò)程,逆向轉(zhuǎn)換過(guò)程被稱(chēng)為逆序列化。
比如在 Python 中,結(jié)構(gòu)化數(shù)據(jù)對(duì)象 dict 并不能直接存儲(chǔ)到文件中:
a = {
"Type": "A",
"field1": "value1",
"field2": "value2",
"field3": "value3",
}
with open('./file.txt', 'w') as f:
f.write(a)
#> TypeError: write() argument must be str, not dict
但是我們可以通過(guò)序列化操作,將 dict 對(duì)象轉(zhuǎn)換為某種可存儲(chǔ)格式。比如可以通過(guò)序列化操作將 dict 對(duì)象轉(zhuǎn)換為字符串序列,從而實(shí)現(xiàn)對(duì) dict 對(duì)象的存儲(chǔ):
a = {
"Type": "A",
"field1": "value1",
"field2": "value2",
"field3": "value3",
}
print(repr(a)) # 可將repr()理解為一種序列化方法
#> {'Type': 'A', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}
with open('./file.txt', 'w') as f:
f.write(repr(a)) # 序列化后的dict對(duì)象,可寫(xiě)入文件
import ast
with open('./file.txt', 'r') as f:
b = ast.literal_eval(f.readline())
print(f'{type(b)}:{b}')
#> :{'Type': 'A', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}
在某些情況下,序列化的第二個(gè)目的是減少數(shù)據(jù)大小,從而減小對(duì)磁盤(pán)和帶寬的要求。
Flat vs. Nested data
在開(kāi)始序列化數(shù)據(jù)之前,我們需要先了解一下序列化之后會(huì)得到何種格式的序列(扁平或嵌套),下面展示了這兩種風(fēng)格的序列:
扁平(flat)風(fēng)格,經(jīng)序列化后的數(shù)據(jù)將位于一個(gè)物理行中:
{ "Type" : "A", "field1": "value1", "field2": "value2", "field3": "value3" }
嵌套(nested)風(fēng)格,經(jīng)序列化后的數(shù)據(jù)會(huì)分散在多個(gè)物理行中:
{"A":
{ "field1": "value1", "field2": "value2", "field3": "value3" } }
序列化格式
"結(jié)構(gòu)化數(shù)據(jù)"經(jīng)序列化后,會(huì)得到兩種序列化格式(serialization format):
文本序列化格式,如 JSON
二進(jìn)制序列化格式,如 pickle
文本序列化格式
簡(jiǎn)單文件(扁平數(shù)據(jù))
repr()
repr() 函數(shù)以單個(gè)對(duì)象作為參數(shù),并生成一個(gè)可被解釋器讀取的字符串表示形式。我們可利用 repr() 將對(duì)象轉(zhuǎn)換為可存儲(chǔ)的扁平風(fēng)格字符串扁平風(fēng)格:
a = {
"Type": "A",
"field1": "value1",
"field2": "value2",
"field3": "value3",
}
print(repr(a)) # 可打印序列化后的內(nèi)容
#> {'Type': 'A', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}
with open('./file.txt', 'w') as f:
f.write(repr(a)) # 可將序列化后的內(nèi)容寫(xiě)入文件
ast.literal_eval()
literal_eval() 方法可安全地解析 Python 數(shù)據(jù)類(lèi)型表達(dá)式并求值,支持的數(shù)據(jù)類(lèi)型有:字符串、數(shù)字、元組、列表、字典、布爾和None。
我們可以使用 literal_eval() 對(duì)簡(jiǎn)單文件中的內(nèi)容進(jìn)行逆序列化,從而獲得 Python 對(duì)象:
import ast
with open('./file.txt', 'r') as f:
inp = ast.literal_eval(f.read())
print(f'{type(inp)}:{inp}')
#> :{'Type': 'A', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}
CSV 文件 (扁平數(shù)據(jù))
Python 中的 CSV 模塊實(shí)現(xiàn)了讀取和寫(xiě)入CSV形式的表格數(shù)據(jù)的類(lèi)。
讀取的簡(jiǎn)單例子:
# 從文件中讀取CSV數(shù)據(jù)
import csv
with open('/tmp/file.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
寫(xiě)入的簡(jiǎn)單例子:
# 將CSV數(shù)據(jù)寫(xiě)入文件
import csv
with open('/temp/file.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(iterable)
該模塊的內(nèi)容、函數(shù)和例子可以在 Python 文檔中 查閱。
YAML (嵌套數(shù)據(jù))
Python 中有許多第三方庫(kù)用來(lái)解析和讀取/寫(xiě)入 YAML 文件,例子如下:
# 使用load方法從文件中讀取 YAML 內(nèi)容
import yaml
with open('/tmp/file.yaml', 'r', newline='') as f:
try:
print(yaml.load(f))
except yaml.YAMLError as ymlexcp:
print(ymlexcp)
第三方庫(kù)的文檔可以在 PyYAML 文檔 中查閱。
JSON 文件 (嵌套數(shù)據(jù))
Python 的 JSON 模塊可以用來(lái)讀取和寫(xiě)入 JSON 模塊。示例如下:
讀取:
# 從文件中讀取 JSON 內(nèi)容
import json
with open('/tmp/file.json', 'r') as f:
data = json.load(f)
寫(xiě)入:
# 使用 dump 方法將 JSON 內(nèi)容寫(xiě)入文件
import json
with open('/tmp/file.json', 'w') as f:
json.dump(data, f, sort_keys=True)
XML (嵌套數(shù)據(jù))
Python 中 XML 的解析可以使用 xml 庫(kù)。
示例:
# 從文件中讀取 XML 內(nèi)容
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
使用 xml.dom 和 xml.sax 包的更多文檔可以在 Python XML 庫(kù)文檔 中找到。
二進(jìn)制序列化格式
NumPy Array (扁平數(shù)據(jù))
Python 的 NumPy 數(shù)組可以將數(shù)據(jù)序列化成字節(jié)形式,或從字節(jié)形式的數(shù)據(jù)反序列化。
示例:
import NumPy as np
# 將 NumPy 數(shù)組轉(zhuǎn)換為字節(jié)形式
byte_output = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]).tobytes()
# 將字節(jié)形式轉(zhuǎn)換回 NumPy 數(shù)組
array_format = np.frombuffer(byte_output)
Pickle (扁平數(shù)據(jù))
Python原生的數(shù)據(jù)序列化模塊稱(chēng)為 Pickle 。
示例:
import pickle
# 示例字典
grades = { 'Alice': 89, 'Bob': 72, 'Charles': 87 }
# 使用 dumps 將對(duì)象轉(zhuǎn)換為序列化字符串
serial_grades = pickle.dumps( grades )
# 使用 loads 反序列化為對(duì)象
received_grades = pickle.loads( serial_grades )
Protobuf
如果您正在尋找支持多種語(yǔ)言的序列化模塊,那么 Google 的 Protobuf 庫(kù)就是一個(gè)選擇。
歡迎關(guān)注公眾號(hào): import hello
總結(jié)
以上是生活随笔為你收集整理的python封装函数、实现将任意的对象序列化到磁盘上_序列化(serialization)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python enumerate用法_【
- 下一篇: python模拟手写笔迹_pytorch