关于python中pickle的用法

为什么要序列化

内存中的字典、列表、集合以及各种对象,如何保存到一个文件中。之前往往是将这些对象转成字符对象然后再写入到文件中。设计一套协议,按照某种规则,把内存中的数据保存到文件中,文件是一个个字节序列。所以必须把数据额转换为字节序列,输出到文件,这就是序列化,反之,从文件的字节 序列恢复到内存中,就是反序列化。

pickle的优点

针对于数据量比较大的列表、字典,可以采用将其加工为数据包来调用,减小文件大小;也就是一个压缩->保存->提取的一个过程。

pickle常用的4个函数

1、pickle.dump(objfileprotocol=None*fix_imports=Truebuffer_callback=None)

把一个对象序列化以二进制写入到文件中

写入方式wb(wb 只写打开或新建一个二进制文件;只允许写数据。)

2、pickle.load(file*fix_imports=Trueencoding="ASCII"errors="strict"buffers=None)

把一个序列化的对象反序列成一个对象

读出方式rb(ab+ 读写打开一个二进制文件,允许读或在文件末追加数据。)

将字典写入文件,然后在从文件中读出。

import pickle

# An arbitrary collection of objects supported by pickle.
data = {
    'a': [1, 2.0, 3, 4+6j],
    'b': ("character string", b"byte string"),
    'c': {None, True, False}
}


#序列化
with open('data.pickle', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)


#反序列化
with open('data.pickle', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    data = pickle.load(f)

print(data)   #{'a': [1, 2.0, 3, (4+6j)], 'b': ('character string', b'byte string'), 'c': {False, True, None}}


将列表写入文件,然后在从文件中读出。 

# An arbitrary collection of objects supported by pickle.

data = [1,2,3,4,5,[6,7,8,9]]
#序列化
with open('data.pickle', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)


#反序列化
with open('data.pickle', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    data = pickle.load(f)

print(data)   #[1, 2, 3, 4, 5, [6, 7, 8, 9]]

 

3、pickle.dumps(objprotocol=None*fix_imports=Truebuffer_callback=None)

对象序列化为bytes对象

直接返回一个对象不进行存储

4、pickle.loads(data*fix_imports=Trueencoding="ASCII"errors="strict"buffers=None)

从bytes对象反序列化成对象

直接返回一个对象不进行存储

import pickle

# An arbitrary collection of objects supported by pickle.

data = [1,2,3,4,5,[6,7,8,9]]
res = pickle.dumps(data)
print(res,type(res))  #b'\x80\x03]q\x00(K\x01K\x02K\x03K\x04K\x05]q\x01(K\x06K\x07K\x08K\tee.' <class 'bytes'>
res = pickle.loads(res)
print(res,type(res))  #[1, 2, 3, 4, 5, [6, 7, 8, 9]] <class 'list'>
import pickle

# An arbitrary collection of objects supported by pickle.

data = {
    'a': [1, 2.0, 3, 4+6j],
    'b': ("character string", b"byte string"),
    'c': {None, True, False}
}
res = pickle.dumps(data)
print(res,type(res))  
# b'\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01]q\x02(K\x01G@\x00\x00\x00\x00\x00\x00\x00K\x03cbuiltins\ncomplex\nq\x03G@\x10\x00\x00\x00\x00\x00\x00G@\x18\x00\x00\x00\x00\x00\x00\x86q\x04Rq\x05eX\x01\x00\x00\x00bq\x06X\x10\x00\x00\x00character stringq\x07C\x0bbyte stringq\x08\x86q\tX\x01\x00\x00\x00cq\ncbuiltins\nset\nq\x0b]q\x0c(\x89\x88Ne\x85q\rRq\x0eu.' <class 'bytes'>
res = pickle.loads(res)
print(res,type(res)) 
# {'a': [1, 2.0, 3, (4+6j)], 'b': ('character string', b'byte string'), 'c': {False, True, None}} <class 'dict'>

总结:pickle这个模板不是一个安全的模块;经常使用dumps和loads。

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值