python对象序列化之pickle

本片文章主要是对pickle官网的阅读记录。

The pickle module implements binary protocols 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 (from a binary file or bytes-like object) is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” [1] or “flattening”; however, to avoid confusion, the terms used here are “pickling” and “unpickling”.

pickle是python标准模块之一,不需要再额外安装。

pickle用来 序列化和反序列化 Python object structure。其实就是一种数据存储方式,将python的数据结构以特定的形式保存下来。另外,经过pickle序列化后的数据不是human-readable的。

这里提一下老外对事物的命名习惯,pickle是腌制的意思,那么对python object的"腌制",其实就是一种数据处理,至于数据处理的规则是什么,这里暂时不做进一步介绍。

“Pickling”  就是将有层次结构的python object转换成字节流;“unpickling” 就是相反的过程。

说明: 如果碰到“Pickling” “serialization”, “marshalling,”  or “flattening”,都是表达相同的意思,翻译成"序列化"就好了;如果单词前加了un,就翻成“反序列化”。

Warning:The pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.

不要去序列化 错误的或者恶意的 结构化数据,也不要去反序列化 不受信任或未授权的数据源。意思就是“序列化”和“反序列化”要按照pickle模块的规则来进行。

Data stream format

The data format used by pickle is Python-specific. This has the advantage that there are no restrictions imposed by external standards such as JSON or XDR (which can’t represent pointer sharing); however it means that non-Python programs may not be able to reconstruct pickled Python objects.

pickle使用的数据格式是Python语言特有的。非Python程序可能不能重构 被序列化 的数据。

By default, the pickle data format uses a relatively compact binary representation. If you need optimal size characteristics, you can efficiently compress pickled data.

默认,pickle的序列化数据格式是一种相对紧凑的二进制表示。如果对数据大小有更高要求,可以压缩 已序列化的数据。

The module pickletools contains tools for analyzing data streams generated by picklepickletools source code has extensive comments about opcodes used by pickle protocols.

pickletools包含很多用来解析 已序列化数据的工具。

There are currently 5 different protocols which can be used for pickling. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.

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

Note:

Serialization is a more primitive notion than persistence; although pickle reads and writes file objects, it does not handle the issue of naming persistent objects, nor the (even more complicated) issue of concurrent access to persistent objects. The pickle module can transform a complex object into a byte stream and it can transform the byte stream into an object with the same internal structure. Perhaps the most obvious thing to do with these byte streams is to write them onto a file, but it is also conceivable to send them across a network or store them in a database. The shelve module provides a simple interface to pickle and unpickle objects on DBM-style database files.

Module Interface

原文地址:https://www.cnblogs.com/black-mamba/p/9092683.html

时间: 2024-08-04 09:42:24

python对象序列化之pickle的相关文章

Python学习笔记12:标准库之对象序列化(pickle包,cPickle包)

计算机的内存中存储的是二进制的序列. 我们能够直接将某个对象所相应位置的数据抓取下来,转换成文本流 (这个过程叫做serialize),然后将文本流存入到文件里. 因为Python在创建对象时,要參考对象的类定义,所以当我们从文本中读取对象时,必须在手边要有该对象的类定义,才干懂得怎样去重建这一对象. 从文件读取时,对于Python的内建(built-in)对象 (比方说整数.词典.表等等),因为其类定义已经加载内存,所以不须要我们再在程序中定义类. 但对于用户自行定义的对象,就必需要先定义类,

python对象序列化或持久化的方法

http://blog.csdn.net/chen_lovelotus/article/details/7233293 一.Python对象持久化方法 目前为止,据我所知,在python中对象持久化有以下几种方法: 1. 使用(dbhash/bsddb, dbm, gdbm, dumbdbm 等)以及它们的"管理器"( anydbm ).只提供了 Python 字 符串的永久性储存. 提供一个类似字典和文件的对象,可以完成字符串的永久性存储. 2. 使用marshal和pickle来序

python 之序列化(pickle模块和json模块)

#!/usr/bin/env python # -*- coding: utf-8 -*- ''' 序列化(pickle)和json 1.什么是序列化? 序列化可以把一个对象(比如列表.字典都是对象),通过python特有的机制序列化一下. 也就是当作是以二进制的形式给它加密一下(特殊二进制的方式加密一下),这个过程就是序列化 并且对一个对象序列化(比如类.列表.字典)之后,可以进行反序列化. ''' import pickle,json li = ['tantianran',11,22,'ok

python数据序列化---json & pickle

json & pickle数据序列化 序列化: 就是列表,字典等数据类型转乘字符串存入文本文件 反序列: 就是字符串从文本读出来后通过一种方法转化为列表,字典等数据类型.例如eval() json通用的:只能处理一些简单的数据类型:json: 序列化和反序列化函数: 案例1:  import json  info = {   "name": "brace",   "age": 22,  }  json_str = json.dumps(i

pickle和cPickle:Python对象的序列化

目的:Python对象序列化 可用性:pickle至少1.4版本,cPickle 1.5版本以上 pickle模块实现了一种算法,将任意一个Python对象转化成一系列字节(byets).此过程也调用了serializing对象.代表对象的字节流之后可以被传输或存储,再重构后创建一个拥有相同特征(the same characteristics)的新的对象. cPickle使用C而不是Python,实现了相同的算法.这比Python实现要快好几倍,但是它不允许用户从Pickle派生子类.如果子类

python 序列化 json pickle

python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象. 基本接口: pickle.dump(obj, file, [,protocol]) 注解:将对象obj保存到文件file中去. protocol为序列化使用的协议版本,0:ASCII协议,所序列化的对象使用可打印的ASCII码表示:1:老式的二进制协议:2:2.3版本引

python 模块 - 序列化 json 和 pickle

1,引入 之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了,所以eval的重点还是通常用来执行一个字符串表达式,并返回表达式的值. import json x = "[nuaa,true,dalse,1]" # print(eval(x)) # 报错,无法解析null类型,而json就可以 print(json.dumps(x)) #

Python学习笔记12:标准库之对象序列化

计算机的内存中存储的是二进制的序列. 我们可以直接将某个对象所对应位置的数据抓取下来,转换成文本流 (这个过程叫做serialize),然后将文本流存入到文件中. 由于Python在创建对象时,要参考对象的类定义,所以当我们从文本中读取对象时,必须在手边要有该对象的类定义,才能懂得如何去重建这一对象. 从文件读取时,对于Python的内建(built-in)对象 (比如说整数.词典.表等等),由于其类定义已经载入内存,所以不需要我们再在程序中定义类. 但对于用户自行定义的对象,就必须要先定义类,

python --- json模块和pickle模块详解

json:JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式(用于数据序列化和反序列化).(适用于多种编程语言,可以与其他编程语言做数据交换) pickle:用于对Python对象结构进行序列化和反序列化.(只适用于python) 对于人类而言,json是人类可读的,而pickle不是. json常用方法(支持列表,字典,元组等基本数据类型): dumps() --- 将传入的对象序列化. 调用:json.dumps(object) 例