python pickle模塊:數據序列化和反序列化

  pickle模塊是python中數據序列化和反序列化的一個包,該模塊提供了一套算法,用於對一個Python對象進行serializing(序列化爲字符串)和de-serializing(從字符串構建對象),這個過程分別叫做pickle和unpickle。 pickle主要提供了四個功能:dumps,loads,dump,load, 兩兩配對使用,下面分別說明 :

dumps、loads函數

函數說明:

dumps(obj, protocal, fix_imports=True)

  這裏主要說明一下,協議:

  • 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 class. 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.

    所以對我來說,我是用的時候都是用 4 或則 3, 因爲我一般也就只用python3, 當然具體環境具體考慮。

loads(byteslike_obj,fix_imports=True,encoding='ASCII', errors='strict')

  這幾個參數比較常見,就不做過多解釋

函數應用:
# _*_ coding: utf-8 _*_
__author__ = 'Jeffery'
__date__ = '2018/7/27 10:13'

import pickle as pkl

a = [[1, 2], [3, 4]]

# fix_imports 主要指python2、python3之間的一個兼容問題
pkl_a = pkl.dumps(a, 4, fix_imports=True)
unpkl_a = pkl.loads(pkl_a, fix_imports=True, encoding='ASCII', errors='strict')

print(pkl_a)
# b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00]\x94(]\x94(K\x01K\x02e]\x94(K\x03K\x04ee.'
print(unpkl_a)
# [[1, 2], [3, 4]]

dump、load函數

  首先,和上面兩個函數的區別是,dump函數是將object序列爲成字符串然後寫入文件,而load函數從文件中讀取反序列化object。所以函數形式也基本和前述類似,只不過dump、load函數參數中多了一個 file參數。

函數應用:

  下面的示例代碼中,我還給出的捕獲異常處理,可以參考使用,靈活應用

__author__ = 'Jeffery'
__date__ = '2018/7/27 10:13'

import pickle as pkl
from pickle import PickleError, PicklingError, UnpicklingError

a = [[1, 2], [3, 4]]
file_path = './pickle_data.pkl'
try:
    with open(file_path, 'wb') as f:
        pkl.dump(a, f, 4, fix_imports=True)
except PicklingError as e:
    print('該對象不可封裝 ', e)
except PickleError as e:
    print('對象封裝過程出錯', e)
except FileNotFoundError as e:
    print('該文件不存在')
except Exception as e:
    print(e)
else:
    with open(file_path, 'rb') as f:
        unpkl_a = pkl.load(f, fix_imports=True, encoding='ASCII', errors='strict')
        print('unpkl_a:', unpkl_a)
finally:
    print('腳本結束')

小結

pickle可以存儲的數據類型有:

  • 所有python支持的原生類型:布爾值,整數,浮點數,複數,字符串,字節,None。
  • 由任何原生類型組成的列表,元組,字典和集合。
  • 函數,類,類的實例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章