Python pickle模塊常用方法代碼實(shí)例
用于序列化的兩個(gè)模塊
- 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語言認(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.
# loads功能 # loads 將pickle數(shù)據(jù)轉(zhuǎn)換為python的數(shù)據(jù)結(jié)構(gòu) mes = pickle.loads(p_str) print(mes) ['aa', 'bb', 'cc']
# dump功能
# dump 將數(shù)據(jù)通過特殊的形式轉(zhuǎn)換為只有python語言認(rèn)識(shí)的字符串,并寫入文件
with open('D:/tmp.pk', 'w') as f:
pickle.dump(data, f)
# load功能
# load 從數(shù)據(jù)文件中讀取數(shù)據(jù),并轉(zhuǎn)換為python的數(shù)據(jù)結(jié)構(gòu)
with open('D:/tmp.pk', 'r') as f:
data = pickle.load(f)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python之基數(shù)排序的實(shí)現(xiàn)
這篇文章主要介紹了python之基數(shù)排序的實(shí)現(xiàn),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Python open讀寫文件實(shí)現(xiàn)腳本
Python中文件操作可以通過open函數(shù),這的確很像C語言中的fopen。通過open函數(shù)獲取一個(gè)file object,然后調(diào)用read(),write()等方法對(duì)文件進(jìn)行讀寫操作。2008-09-09
python pandas dataframe 去重函數(shù)的具體使用
這篇文章主要介紹了python pandas dataframe 去重函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
解決python中顯示圖片的plt.imshow plt.show()內(nèi)存泄漏問題
這篇文章主要介紹了解決python中顯示圖片的plt.imshow plt.show()內(nèi)存泄漏問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python更新數(shù)據(jù)庫腳本兩種方法及對(duì)比介紹
這篇文章給大家介紹了Python更新數(shù)據(jù)庫腳本兩種方法及數(shù)據(jù)庫查詢?nèi)N方式,然后在文章下面給大家介紹了兩種方式對(duì)比介紹,非常不錯(cuò),感興趣的朋友參考下吧2017-07-07
python datetime時(shí)間格式的相互轉(zhuǎn)換問題
這篇文章主要介紹了python datetime時(shí)間格式的相互轉(zhuǎn)換問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
教你用Python創(chuàng)建微信聊天機(jī)器人
這篇文章主要手把手教你用Python創(chuàng)建微信聊天機(jī)器人,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

