Pickle-------python对象序列化

本文主要阐述以下几点:

  1.pickle模块简介

  2.pickle模块提供的方法

  3.注意事项

  4.实例解析

1.pickle模块简介

The pickle module implements a fundamental, but powerful algorithm for serializing(序列化) and de-serializing(反序列化) a Python object structure.
“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy.

        

                  上面图示说明了pickle模块的功能

  那么为什么需要序列化和反序列化这一操作呢?

  1.便于存储。序列化过程将文本信息转变为二进制数据流。这样就信息就容易存储在硬盘之中,当需要读取文件的时候,从硬盘中读取数据,然后再将其反序列化便可以得到原始的数据。在Python程序运行中得到了一些字符串、列表、字典等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就丢失数据。python模块大全中的Pickle模块就派上用场了,它可以将对象转换为一种可以传输或存储的格式。

  2.便于传输。当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把這个对象转换为字节序列,在能在网络上传输;接收方则需要把字节序列在恢复为对象。

2.pickle模块提供的方法(python2.x与python3.x)

 先来看看python2.x提供的方法,然后通过源码来看看两着的区别。

pickle.dump(obj, file[, protocol]) #将pickle对象保存至文件
  Write a pickled representation of obj to the open file object file.
    #将一个pickled对象写入到文件对象中。等价于Pickler(file, protocol).dump(obj)
  If the protocol parameter is omitted, protocol 0 is used. 
#如果协议参数省略,默认为0,这里的协议参数我们先不讨论
    file must have a write() method that accepts a single string argument.
#注意这里。文件必须具有可写权限

  由上看出dump方法是将一个被pickle的字符串写入到文件中保存。这种方法可以用在保存用户密码,通过dump方法写入文件保存。

pickle.load(file) #从文件读取数据,边读取边反序列
 Read a string from the open file object file and interpret it as a  pickle data stream, reconstructing and returning the original object hierarchy. 
  #从文件对象读取字符串,同时将其反序列化输出,这个方法等价于Unpickler(file).load().
 file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. 
 #文件必须可读.其中read()方法必须接受一个整数参数,来 确定读取的行数;readline()可不必设置参数
This function automatically determines whether the data stream was written in binary mode or not.
 #这个函数能够自动识别写入的数据是否为二进制形式

  以上两个方法是对文件的序列化和反序列化,切记不要和以下两个方法弄混了。

pickle.dumps(obj[, protocol])
  Return the pickled representation of the object as a string, instead of writing it to a file.#序列化字符串对象,但不写入到文件

  If the protocol parameter is omitted, protocol 0 is used. 

pickle.loads(string)
  Read a pickled object hierarchy from a string. 

  总结:1.如果想把序列化的字符串写入文件,用load()和dump()方法。

     2.序列化到内存,可以采用loads()和dumps()方法。

  下面来看看python3.x提供的四种方法,我们重点关注它们与Python2.x提供方法的区别。

pickle.dump(obj, file, protocol=None, *, fix_imports=True)
  Write a pickled representation of obj to the open file object file. 等价于Pickler(file, protocol).dump(obj).#保存至文件
  
  The file argument must have a write() method that accepts a single bytes argument.
pickle.dumps(obj, protocol=None, *, fix_imports=True)
  Return the pickled representation of the object as a bytes object, instead of writing it to a file.

pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict")
  Read a pickled object representation from the open file object file and return the reconstituted object hierarchy specified therein. 
 # 等价于 Unpickler(file).load().
 The argument file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. 

Both methods should return bytes. #read()和readline()返回字节 Optional keyword arguments are fix_importsre, encoding and errors.#重点来关注编码

The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects. pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict") Read a pickled object hierarchy from a bytes object and return the reconstituted object hierarchy specified therein.
similar to pickle.load()

  总结:

3.注意事项

  哪些可以被序列化和反序列化? 

None, True, and False
整数, 长整数, 浮点数, 复合整数
normal and Unicode strings
tuples, lists, sets, and dictionaries containing only picklable objects(也就是说这三种类型可嵌套)
functions defined at the top level of a module#定义在顶层模块的函数
built-in functions defined at the top level of a module#定义在顶层模块的内置函数
classes that are defined at the top level of a module#定义在顶层模块的类
instances of such classes whose __dict__ or the result of calling __getstate__() is picklable (see section The pickle protocol for details).  

4.实例解析

#example1
import pickle
info = [1,2,3,4,'asd']
data1 = pickle.dumps(info)
data2 = pickle.loads(data1)

print data1
print data2
#example2
import pickle entry = {'a' : 11,'b' : 22} with open('entry.pickle','wb') as f : pickle.dump(entry,f) #序列化到文件 with open('entry.pickle','rb') as f: entry = pickle.load(f) #从文件中反序列化出数据 print entry
#序列化
import pickle

data1 = {'a': [1, 2.0, 3, 4+6j],
         'b': ('string', u'Unicode string'),
         'c': None}

selfref_list = [1, 2, 3]
selfref_list.append(selfref_list)

output = open('data', 'wb')

# Pickle dictionary using protocol 0.
pickle.dump(data1, output)

# Pickle the list using the highest protocol available.
pickle.dump(selfref_list, output, -1)

output.close()
#反序列化 import pprint, pickle pkl_file = open('data', 'rb') data1 = pickle.load(pkl_file) pprint.pprint(data1) data2 = pickle.load(pkl_file) pprint.pprint(data2) pkl_file.close()
reader = TextReader("hello.txt")
>>> reader.readline()
'1: Hello world!'
>>> reader.readline()
'2: I am line number two.'
>>> new_reader = pickle.loads(pickle.dumps(reader))
>>> new_reader.readline()
'3: Goodbye!'

  关于pickle模块先暂时介绍到这里,关于pickle类待更新!!!

  

下一篇:[python标准库]JSON模块http://www.cnblogs.com/vipchenwei/p/6951455.html

  

[python标准库]Pickle模块的更多相关文章

  1. Python 标准库 ConfigParser 模块 的使用

    Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...

  2. Python标准库——collections模块的Counter类

    1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...

  3. Python标准库 (pickle包,cPickle包)

    在之前对Python对象的介绍中 (面向对象的基本概念,面向对象的进一步拓展),我提到过Python“一切皆对象”的哲学,在Python中,无论是变量还是函数,都是一个对象.当Python运行时,对象 ...

  4. [python标准库]XML模块

    1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...

  5. 【python】Python标准库defaultdict模块

    来源:http://www.ynpxrz.com/n1031711c2023.aspx Python标准库中collections对集合类型的数据结构进行了很多拓展操作,这些操作在我们使用集合的时候会 ...

  6. Python标准库--os模块

    这个模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下运行.一个例 ...

  7. python标准库 bisect模块

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #bisect #作用:维护有序列表,而不必在每次向列表增加一个元素 ...

  8. python标准库 sysconfig模块

    # -*- coding: utf-8 -*-# python:2.x__author__ = 'Administrator'import sysconfig#sysconfig:解释器编译时配置#作 ...

  9. Python标准库 -- UUID模块(生成唯一标识)

    UUID是什么: UUID: 通用唯一标识符 ( Universally Unique Identifier ),对于所有的UUID它可以保证在空间和时间上的唯一性,也称为GUID,全称为: UUID ...

随机推荐

  1. 学习css之文本属性

    css3之文本属性: 1.缩进和水平对齐:text-indent, 通过使用 text-indent 属性,所有元素的第一行都可以缩进一个给定的长度,甚至该长度可以是负值. 这个属性最常见的用途是将段 ...

  2. VS2003"无法启动调试 没有正确安装调试器"的解决方法

    在用VS2003做项目的时候,经常调试程序,但是有时候回出现如下问题“无法启动调试,没有正确安装调试器,请运行安装程序或修复调试器”.第一次碰到还以为是运气不好,就重新用vs2003安装程序重新修复了 ...

  3. JavaScript 小函数积累及性能优化

    获取值的类型: var toString = Object.prototype.toString; function getType(o) { return toString.call(o).slic ...

  4. 【caffe-windows】 caffe-master 之 训练自己数据集(图片转换成lmdb or leveldb)

    前期准备: 文件夹train:此文件夹中按类别分好子文件夹,各子文件夹里存放相应图片 文件夹test:同train,有多少类就有多少个子文件夹 trainlabels.txt : 存的是训练集的标签  ...

  5. cookie,session,token的定义及区别

    参考了很多文章总结的. 1.cookie(储存在用户本地终端上的数据) 服务器生成,发送给浏览器,浏览器保存,下次请求同一网站再发送给服务器. 2.session(会话) a.代表服务器与浏览器的一次 ...

  6. 初识Android触摸事件传递机制

    前言 今天总结的一个知识点是Andorid中View事件传递机制,也是核心知识点,相信很多开发者在面对这个问题时候会觉得困惑,另外,View的另外一个难题滑动冲突,比如在ScrollView中嵌套Li ...

  7. 磁盘IO:缓存IO与直接IO

    文件系统IO分为DirectIO和BufferIO,其中BufferIO也叫Normal IO. 1. 缓存IO 缓存I/O又被称作标准I/O,大多数文件系统的默认I/O操作都是缓存I/O.在Linu ...

  8. 一起来学jquery!

    jquery学习之旅!(未完待续) ------------(一)jquery书写步骤 ------------(二)jquery事件与函数 ------------(三)jquery修改css属性 ...

  9. 微软 Build 2017 开发者大会:Azure 与 AI 的快速发展

    欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~ 一年一度的微软 Build 大会准时起航,本年度大会从旧金山移师西雅图,一个近年来凭借女神汤唯而在中国家喻户晓的美国西部海滨城市 ...

  10. RNN的介绍

    一.状态和模型 在CNN网络中的训练样本的数据为IID数据(独立同分布数据),所解决的问题也是分类问题或者回归问题或者是特征表达问题.但更多的数据是不满足IID的,如语言翻译,自动文本生成.它们是一个 ...