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。
  • 由任何原生类型组成的列表,元组,字典和集合。
  • 函数,类,类的实例

猜你喜欢

转载自blog.csdn.net/jeffery0207/article/details/81233627