笔记-python-standard library-12.1 pickle
笔记-python-standard library-12.1 pickle
1. pickle简介
source code: Lib/pickle.py
pickle模块实质上是一个实现python对象结构序列化的二进制协议。可以“序列化”,当然也可以“反序列化“。
python中也有一些其它的模块实现类似的功能,但一般情况下序列化使用pickle:
- marshal
主要用于支持.pyc文件。考虑到扩展性以及功能性,一般情况下序列化使用Pickle。
- json
json是一种文本序列化格式,是人类可读的,而Pickle不是。
关键是json支持的Python对象类型仅限于内置类型。
1.1. 数据流格式
pickle使用的数据格式是python特定的。好处是不与其它协议冲突,坏处是其它协议不支持pickle格式。
默认情况下,pickle数据数据格式使用较高压缩率的二进制格式,当然,可以指定压缩率。
有5种协议用于序列化,越新的协议版本号越大,同样,需要新版的python。支持:
- version 0
最原始的“人类可读”版协议,与早期的python版本兼容。
- version 1
旧的二进制格式,与早期的Python兼容。
- version 2 python 2.3中引进。
- version 3 Python3.0 中引进。
- version 4 python 3.4中引进。
1.2. 模块接口
序列化使用dumps(),反序列化使用loads()。如果需要进行更多的细节控制,可以使用Pickler和Unpickler对象。
模块常量:
- pickle.HIGHEST_PROTOCOL
整数,支持协议的最高版本。
- pickle.DEFAULT_PROTOCOL
整数,默认协议版本。目前默认是3.
方法:
- pickle.dump(obj, file, protocol=None, *, fix_imports=True)
将对象序列化并写入文件句柄。
protocol,一个可选参数,整数,使用指定版本协议。
file参数必需有支持字节写入的write()方法,
fix_imports主要用于与python2的兼容,一般不用设置。
- pickle.dumps(obj, protocol=None, *, fix_imports=True)
将对象序列化并返回一个二进制对象。
- pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict")
从指定文件对象中读取序列化对象。
协议版本自动查看。
- pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
对应于dumps。
1.2.1. 代码案例
import pickle
from json import load
a = pickle.HIGHEST_PROTOCOL
print(a)
a = pickle.DEFAULT_PROTOCOL
print(a)
with open('nums.txt', 'r', encoding='utf-8') as fi:
lisa = load(fi)
print(len(lisa))
file_a = 'pickle_a.txt'
with open(file_a, 'wb+') as fi:
pass
pickle.dump(lisa,fi)
file_a = 'pickle_a.txt'
with open(file_a, 'rb+') as fi:
pass
pickle.dump(lisa,fi)
1.3. 异常
pickle模块定义了三种异常:
- exception pickle.PickleError
pickle模块异常类的基类,继承于Exception.
- exception pickle.PicklingError 序列化失败时抛出
- exception pickle.UnpicklingError 反序列化失败时抛出
1.4. class
- class pickle.Pickler(file, protocol=None, *, fix_imports=True)
This takes a binary file for writing a pickle data stream.
methods:
dump(obj) 将obj pickle化并写入file
- class pickle.Unpickler(file, *, fix_imports=True, encoding="ASCII", errors="strict")
This takes a binary file for reading a pickle data stream.
load() 从文件句柄中读取pikled对象并反序列化。返回反序列化后的对象。
1.5. pickled 和unpickled范围
下面这些对象是可以序列化和反序列化的。
None, True, and False
integers, floating point numbers, complex numbers
strings, bytes, bytearrays
tuples, lists, sets, and dictionaries containing only picklable objects
functions defined at the top level of a module (using def, not lambda)
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 Pickling Class Instances for details).
笔记-python-standard library-12.1 pickle的更多相关文章
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- The Python Standard Library
The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...
- Python语言中对于json数据的编解码——Usage of json a Python standard library
一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...
- 《The Python Standard Library》——http模块阅读笔记1
官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...
- 《The Python Standard Library》——http模块阅读笔记2
http.server是用来构建HTTP服务器(web服务器)的模块,定义了许多相关的类. 创建及运行服务器的代码一般为: def run(server_class=HTTPServer, handl ...
- 《The Python Standard Library》——http模块阅读笔记3
http.cookies — HTTP state management http.cookies模块定义了一系列类来抽象cookies这个概念,一个HTTP状态管理机制.该模块支持string-on ...
- Python Standard Library 学习(一) -- Built-in Functions 内建函数
内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
- C++11新特性——The C++ standard library, 2nd Edition 笔记(一)
前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...
- [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II
[译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...
随机推荐
- webservice 简单实例
C# 创建.部署和调用WebService的简单示例 webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互. 概念性的东西就不说太多,下面开始创建一个简单的webservi ...
- Android Asynchronous Http Client
Features Make asynchronous HTTP requests, handle responses in anonymous callbacks HTTP requests happ ...
- 《大话设计模式》num03-04-05---单一职责原则、开放封闭原则、依赖倒转原则
2018年03月03日 21:19:19 独行侠的守望 阅读数个人分类: 设计模式 版权声明:本文为博主原创文章,转载请注明文章链接. https://blog.csdn.net/xiaoanzi12 ...
- spring-boot整合shiro作权限认证
spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说 引入pom文件 <!--shiro集成spring--& ...
- TCP的连接和释放过程
TCP的连接和释放过程 1.三次握手的过程 1)主机A向主机B发送TCP连接请求数据包,其中包含主机A的初始序列号seq(A)=x.(其中报文中同步标志位SYN=1,ACK=0,表示这是一个TCP连接 ...
- Customers Who Never Order
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...
- #include stdio.h(2)
#include <stdio.h> //mian函数是程序的入口 int main() { /* //函数:是按一定的格式对一段代码的封装 //专门用来实现一功能的代码合集,可以重复使用 ...
- html便民查询各个工具类实例代码分享(支持pc和移动端)
1.手机号码查询 <iframe id="api_iframe_51240" name="api_iframe_51240" src="&quo ...
- Spring @Autowired使用介绍
参考博客: https://blog.csdn.net/u013412772/article/details/73741710 引用文章地址: https://my.oschina.net/Helio ...
- 如何使用VS将项目生成一个安装包?
VS2010项目的部署与安装winform程序,我想进行安装.1.在解决方案中 ——点击右键——添加 2.然后选择 安装和部署 ——安装向导 可以更改名称 3.点击 下一步 4.然后选择上那3个 5. ...