欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中的序列化詳細(xì)解析

 更新時間:2023年11月29日 09:15:02   作者:時代&信念  
這篇文章主要介紹了Python中的序列化詳細(xì)解析,序列化是指把程序中的一個類轉(zhuǎn)化成一個標(biāo)準(zhǔn)化的格式,標(biāo)準(zhǔn)化的意義是這個格式可以跨程序,跨平臺的被使用,而且保持其原有的內(nèi)容,規(guī)范,需要的朋友可以參考下

1.什么是數(shù)據(jù)序列化?

序列化 (Serialization),是指把程序中的一個類轉(zhuǎn)化成一個標(biāo)準(zhǔn)化的格式。

標(biāo)準(zhǔn)化的意義是這個格式可以跨程序,跨平臺的被使用,而且保持其原有的內(nèi)容,規(guī)范。

2.為什么要進(jìn)行數(shù)據(jù)序列化呢?

(1)一致性

我們將要保存的數(shù)據(jù),序列化成標(biāo)準(zhǔn)的格式(Json格式或者Pickle格式)。之后再反序列化回來,數(shù)據(jù)依然是原來的。保持了數(shù)據(jù)的一致性。

(2)有效性

序列化之后,可以減少內(nèi)存和網(wǎng)絡(luò)資源的占用。

(3)兼容性

將數(shù)據(jù)序列化之后,Json格式或者Pickle格式,我可以在其他平臺(其他操作系統(tǒng)的電腦)上依然使用。 我用python對數(shù)據(jù)進(jìn)行了序列化,我之后可以使用java等其他語言,對其進(jìn)行反序列,然后進(jìn)行使用,數(shù)據(jù)并沒有發(fā)生改變。

3.數(shù)據(jù)序列化的應(yīng)用

(1)應(yīng)用一

你的程序需要和其他程序交流,例如 平臺 API, 網(wǎng)頁請求

在這里插入圖片描述

(2)應(yīng)用二

串行任務(wù)流,每一個任務(wù)結(jié)束之后數(shù)據(jù)通過序列化傳遞到下一個任務(wù)。

在這里插入圖片描述

4.JSON

JSON 是一個文件格式,也是一個標(biāo)準(zhǔn)化的數(shù)據(jù)傳輸方案,通常網(wǎng)站的后端和前端的交流,移動 APP 和云服務(wù)器的交流方式都是通過 JSON。

(1)序列化

# 導(dǎo)入json模塊
import json

simple_dict = {'name': 'zxy', 'age': 21}
with open('simple_dict.txt', 'w') as file_to_write:
    # 進(jìn)行json序列化,然后寫入simple_dict.txt文件中
    json.dump(simple_dict, file_to_write)

(2)反序列化

with open('simple_dict.txt', 'r') as file_to_read:
    loaded_simple_dict = json.load(file_to_read)
    print(loaded_simple_dict)
    print(type(loaded_simple_dict))

在這里插入圖片描述

(3)Json 方法的弊端

當(dāng)遇到一些 Python 特定的高級數(shù)據(jù)類型的時候,Json 會因為沒有標(biāo)準(zhǔn)而無法進(jìn)行序列化。 會報如下錯誤:

在這里插入圖片描述

5.Pickle

Pickle 和 Json 不同的是,Pickle 是 Python 專屬的序列化方案,可以轉(zhuǎn)化大多數(shù) Python 的數(shù)據(jù)類型,并且儲存方式是二進(jìn)制(Byte Code)。二進(jìn)制的儲存方式只有機器才能理解,但是同時也保證了一定的數(shù)據(jù)隱秘性和高效性。

(1)序列化

import pickle
import datetime
abc_dict = {datetime.datetime(2019, 7, 18, 0, 0): 9682.24,
            datetime.datetime(2019, 7, 17, 0, 0): 9411.61,
            datetime.datetime(2019, 7, 16, 0, 0): 10858.7,
            datetime.datetime(2019, 7, 15, 0, 0): 10195.0,
            datetime.datetime(2019, 7, 14, 0, 0): 11378.23,
            datetime.datetime(2019, 7, 13, 0, 0): 11810.0,
            datetime.datetime(2019, 7, 12, 0, 0): 11338.9,
            datetime.datetime(2019, 7, 11, 0, 0): 12090.99,
            datetime.datetime(2019, 7, 10, 0, 0): 12577.85}

with open('abc.pk', 'wb') as file_to_write:
    pickle.dump(abc_dict, file_to_write)

二進(jìn)制的儲存方式只有機器才能理解,保證了一定的數(shù)據(jù)隱秘性和高效性。

在這里插入圖片描述

(2)反序列化

import pickle
import datetime
abc_dict = {datetime.datetime(2019, 7, 18, 0, 0): 9682.24,
            datetime.datetime(2019, 7, 17, 0, 0): 9411.61,
            datetime.datetime(2019, 7, 16, 0, 0): 10858.7,
            datetime.datetime(2019, 7, 15, 0, 0): 10195.0,
            datetime.datetime(2019, 7, 14, 0, 0): 11378.23,
            datetime.datetime(2019, 7, 13, 0, 0): 11810.0,
            datetime.datetime(2019, 7, 12, 0, 0): 11338.9,
            datetime.datetime(2019, 7, 11, 0, 0): 12090.99,
            datetime.datetime(2019, 7, 10, 0, 0): 12577.85}

with open('abc.pk', 'wb') as file_to_write:
    # pickle序列化,然后以二進(jìn)制的形式存入文件中
    pickle.dump(abc_dict, file_to_write)

with open('abc.pk', 'rb') as file_to_read:
    # 以二進(jìn)制的形式進(jìn)行讀取文件
    abc_dict_pk = pickle.load(file_to_read)
    print(abc_dict_pk)
    print(type(abc_dict_pk))

在這里插入圖片描述

6.總結(jié)

這里兩個方法的行為都是在序列化數(shù)據(jù),所以在調(diào)用函數(shù)上感覺完全一樣。

但是本質(zhì)上 Json 寫入文件的是字符串,而 Pickle 則是把數(shù)據(jù)轉(zhuǎn)化成了二進(jìn)制,兩個是完全不同的處理方案。

到此這篇關(guān)于Python中的序列化詳細(xì)解析的文章就介紹到這了,更多相關(guān)Python中的序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論