博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python模块之pickle(列表,字典等复杂数据类型与二进制文件的转化)
阅读量:5050 次
发布时间:2019-06-12

本文共 4843 字,大约阅读时间需要 16 分钟。

1.pickle模块简介

The module implements binary protocols for serializing and de-serializing a Python object structure。

大意是说:pickle模块是用来实现二进制存储对于python对象结构的的序列化和反序列化。

2.使用前导入模块

import pickle

3.创造要序列化的数据结构

如列表:my_list = [123,3.14,'小甲鱼',['another list',20]]

4.创建一个二进制文件

pick_file = open('D:\\test\\my_list.pkl','wb')

5.使用pickle的函数dump装入文件

 pickle.dump(my_list,pick_file)

6.关闭打开的文件完成写入

 pick_file.close()

7.运行结果,由于写入的是二进制文件,那么用文本打开肯定是乱码的

8.以指读方式打开刚才存储的二进制文件

pickfile = open('D:\\test\\my_list.pkl','rb')

9.读取文件内容到列表,怎么写入的怎么读取

listfile = pickle.load(pickfile)

10.查看读取结果,我们写入的内容又被我们读取出来了

>>> print(listfile)

[123, 3.14, '小甲鱼', ['another list', 20]]

 11.介绍文档

NAME

pickle - Create portable serialized representations of Python objects.

DESCRIPTION

See module copyreg for a mechanism for registering custom picklers.
See module pickletools source for extensive comments.
Classes:
Pickler
Unpickler
Functions:
dump(object, file)
dumps(object) -> string         
load(file) -> object
loads(string) -> object
Misc variables:
__version__
format_version
compatible_formats

FUNCTIONS

dump(obj, file, protocol=None, *, fix_imports=True)
Write a pickled representation of obj to the open file object file.
This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
be more efficient.
The optional *protocol* argument tells the pickler to use the given
protocol supported protocols are 0, 1, 2, 3 and 4. The default
protocol is 3; a backward-incompatible protocol designed for Python 3.
Specifying a negative protocol version selects the highest protocol
version supported. The higher the protocol used, the more recent the
version of Python needed to read the pickle produced.
The *file* argument must have a write() method that accepts a single
bytes argument. It can thus be a file object opened for binary
writing, an io.BytesIO instance, or any other custom object that meets
this interface.
If *fix_imports* is True and protocol is less than 3, pickle will try
to map the new Python 3 names to the old module names used in Python
2, so that the pickle data stream is readable with Python 2.
dumps(obj, protocol=None, *, fix_imports=True)
Return the pickled representation of the object as a bytes object.
The optional *protocol* argument tells the pickler to use the given
protocol; supported protocols are 0, 1, 2, 3 and 4. The default
protocol is 3; a backward-incompatible protocol designed for Python 3.
Specifying a negative protocol version selects the highest protocol
version supported. The higher the protocol used, the more recent the
version of Python needed to read the pickle produced.
If *fix_imports* is True and *protocol* is less than 3, pickle will
try to map the new Python 3 names to the old module names used in
Python 2, so that the pickle data stream is readable with Python 2.
load(file, *, fix_imports=True, encoding='ASCII', errors='strict')
Read and return an object from the pickle data stored in a file.
This is equivalent to ``Unpickler(file).load()``, but may be more
efficient.
The protocol version of the pickle is detected automatically, so no
protocol argument is needed. Bytes past the pickled object's
representation are ignored.
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. Thus *file* can be a
binary file object opened for reading, an io.BytesIO object, or any
other custom object that meets this interface.
Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
which are used to control compatibility support for pickle stream
generated by Python 2. If *fix_imports* is True, pickle will try to
map the old Python 2 names to the new names used in Python 3. The
*encoding* and *errors* tell pickle how to decode 8-bit string
instances pickled by Python 2; these default to 'ASCII' and 'strict',
respectively. The *encoding* can be 'bytes' to read these 8-bit
string instances as bytes objects.
loads(data, *, fix_imports=True, encoding='ASCII', errors='strict')
Read and return an object from the given pickle data.
The protocol version of the pickle is detected automatically, so no
protocol argument is needed. Bytes past the pickled object's
representation are ignored.
Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
which are used to control compatibility support for pickle stream
generated by Python 2. If *fix_imports* is True, pickle will try to
map the old Python 2 names to the new names used in Python 3. The
*encoding* and *errors* tell pickle how to decode 8-bit string
instances pickled by Python 2; these default to 'ASCII' and 'strict',
respectively. The *encoding* can be 'bytes' to read these 8-bit
string instances as bytes objects.

 

posted on
2017-12-31 17:41 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/huipengbo/p/8158245.html

你可能感兴趣的文章
牛客带你学编程-Java测试卷
查看>>
hdoj1051
查看>>
poj2318
查看>>
07-图4 哈利·波特的考试
查看>>
iOS学习之iOS程序名称及内容国际化(本地化)
查看>>
生产案例、Linux出现假死,怎么回事?
查看>>
树结构(三)---- 多路查找树
查看>>
07深入理解C指针之---指针类型和长度
查看>>
06深入理解C指针之---指针操作和比较
查看>>
SQL Server发送邮件的存储过程
查看>>
【20160924】GOCVHelper 图像处理部分(3)
查看>>
HashMap底层原理
查看>>
js/jQuery实现类似百度搜索功能
查看>>
【转】排序算法复习(Java实现)(一): 插入,冒泡,选择,Shell,快速排序
查看>>
myeclipse中修改servlet的模板代码
查看>>
js 倒计时10s
查看>>
再次陷入迷惘期的一点感想
查看>>
字符串的常用方法
查看>>
SQL查询语句 常用示例
查看>>
抄写例题作业1
查看>>