日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Python模块之pickle(列表,字典等复杂数据类型与二进制文件的转化)

發(fā)布時間:2024/4/24 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 Python模块之pickle(列表,字典等复杂数据类型与二进制文件的转化) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.pickle模塊簡介

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

大意是說:pickle模塊是用來實現(xiàn)二進(jìn)制存儲對于python對象結(jié)構(gòu)的的序列化和反序列化。

2.使用前導(dǎo)入模塊

import pickle

3.創(chuàng)造要序列化的數(shù)據(jù)結(jié)構(gòu)

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

4.創(chuàng)建一個二進(jìn)制文件

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

5.使用pickle的函數(shù)dump裝入文件

pickle.dump(my_list,pick_file)

6.關(guān)閉打開的文件完成寫入

pick_file.close()

7.運(yùn)行結(jié)果,由于寫入的是二進(jìn)制文件,那么用文本打開肯定是亂碼的

8.以指讀方式打開剛才存儲的二進(jìn)制文件

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

9.讀取文件內(nèi)容到列表,怎么寫入的怎么讀取

listfile = pickle.load(pickfile)

10.查看讀取結(jié)果,我們寫入的內(nèi)容又被我們讀取出來了

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

總結(jié)

以上是生活随笔為你收集整理的Python模块之pickle(列表,字典等复杂数据类型与二进制文件的转化)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。