Python序列化pickle模塊使用詳解-創(chuàng)新互聯(lián)

用于序列化的兩個(gè)模塊

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為岑溪企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站建設(shè),岑溪網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
  •   json:用于字符串和Python數(shù)據(jù)類型間進(jìn)行轉(zhuǎn)換
  •   pickle: 用于python特有的類型和python的數(shù)據(jù)類型間進(jìn)行轉(zhuǎn)換
  •   json提供四個(gè)功能:dumps,dump,loads,load
  •   pickle提供四個(gè)功能:dumps,dump,loads,load

pickle可以存儲(chǔ)什么類型的數(shù)據(jù)呢?

所有python支持的原生類型:布爾值,整數(shù),浮點(diǎn)數(shù),復(fù)數(shù),字符串,字節(jié),None。


由任何原生類型組成的列表,元組,字典和集合。


函數(shù),類,類的實(shí)例


pickle模塊中常用的方法有:


1. pickle.dump(obj, file, protocol=None,)

必填參數(shù)obj表示將要封裝的對(duì)象

必填參數(shù)file表示obj要寫入的文件對(duì)象,file必須以二進(jìn)制可寫模式打開,即“wb”

可選參數(shù)protocol表示告知pickler使用的協(xié)議,支持的協(xié)議有0,1,2,3,默認(rèn)的協(xié)議是添加在Python 3中的協(xié)議3?!  ?/p>

  • Protocol version 0 is the original “human-readable” protocol and is backwards compatible with earlier versions of Python.
  • Protocol version 1 is an old binary format which is also compatible with earlier versions of Python.
  • Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes. Refer to PEP 307 for information about improvements brought by protocol 2.
  • Protocol version 3 was added in Python 3.0. It has explicit support for bytes objects and cannot be unpickled by Python 2.x. This is the default protocol, and the recommended protocol when compatibility with other Python 3 versions is required.
  • Protocol version 4 was added in Python 3.4. It adds support for very large objects, pickling more kinds of objects, and some data format optimizations. Refer to PEP 3154 for information about improvements brought by protocol 4.

2. pickle.load(file,*,fix_imports=True, encoding="ASCII", errors="strict")

必填參數(shù)file必須以二進(jìn)制可讀模式打開,即“rb”,其他都為可選參數(shù)

3. pickle.dumps(obj):以字節(jié)對(duì)象形式返回封裝的對(duì)象,不需要寫入文件中

4. pickle.loads(bytes_object): 從字節(jié)對(duì)象中讀取被封裝的對(duì)象,并返回

pickle模塊可能出現(xiàn)三種異常:

1. PickleError:封裝和拆封時(shí)出現(xiàn)的異常類,繼承自Exception

2. PicklingError: 遇到不可封裝的對(duì)象時(shí)出現(xiàn)的異常,繼承自PickleError

3. UnPicklingError: 拆封對(duì)象過程中出現(xiàn)的異常,繼承自PickleError

應(yīng)用:

# dumps功能
import pickle
data = ['aa', 'bb', 'cc'] 
# dumps 將數(shù)據(jù)通過特殊的形式轉(zhuǎn)換為只有python語(yǔ)言認(rèn)識(shí)的字符串
p_str = pickle.dumps(data)
print(p_str)   7 b'\x80\x03]q\x00(X\x02\x00\x00\x00aaq\x01X\x02\x00\x00\x00bbq\x02X\x02\x00\x00\x00ccq\x03e.

網(wǎng)頁(yè)標(biāo)題:Python序列化pickle模塊使用詳解-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article28/ceojcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站營(yíng)銷、網(wǎng)站設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)