python pickle模块

Posted 魂~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pickle模块相关的知识,希望对你有一定的参考价值。

  1 import pickle
  2 
  3 # 在python中如果我们有一些对象需要持久性存储,并且不丢失我们这个对象的类型与数据,
  4 # 我们则需要将这些对象进行序列化,序列化后,需要使用的时候,我们在回复为原来的数据,
  5 # 序列化的这种过程,我们将其称为pickle(腌制)
  6 
  7 
  8 # 1、dumps(object)  python对象 --> 字符串
  9 list_a = ["mingyue", "jishi", "you"]
 10 list_b = pickle.dumps(list_a)
 11 print(list_b)
 12 
 13 
 14 # 2、loads(string)  字符串 --> python对象
 15 list_c = pickle.loads(list_b)
 16 print(list_c)
 17 
 18 
 19 # 3、dump(object, file)  python对象 --> 文件
 20 group1 = ("baidu", "wen", "qingtian")
 21 f1 = open("1.pk1", "wb")
 22 pickle.dump(group1, f1, True)
 23 f1.close()
 24 
 25 # 4、load(object, file)  文件 --> python对象
 26 f2 = open("1.pk1", "rb")
 27 t = pickle.load(f2)
 28 print(t)
 29 f2.close()
 30 
 31 """
 32     dump(obj, file, protocol=None, *, fix_imports=True)
 33         Write a pickled representation of obj to the open file object file.
 34 
 35         This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
 36         be more efficient.
 37 
 38         The optional *protocol* argument tells the pickler to use the given
 39         protocol supported protocols are 0, 1, 2, 3 and 4.  The default
 40         protocol is 3; a backward-incompatible protocol designed for Python 3.
 41 
 42         Specifying a negative protocol version selects the highest protocol
 43         version supported.  The higher the protocol used, the more recent the
 44         version of Python needed to read the pickle produced.
 45 
 46         The *file* argument must have a write() method that accepts a single
 47         bytes argument.  It can thus be a file object opened for binary
 48         writing, an io.BytesIO instance, or any other custom object that meets
 49         this interface.
 50 
 51         If *fix_imports* is True and protocol is less than 3, pickle will try
 52         to map the new Python 3 names to the old module names used in Python
 53         2, so that the pickle data stream is readable with Python 2.
 54 
 55     dumps(obj, protocol=None, *, fix_imports=True)
 56         Return the pickled representation of the object as a bytes object.
 57 
 58         The optional *protocol* argument tells the pickler to use the given
 59         protocol; supported protocols are 0, 1, 2, 3 and 4.  The default
 60         protocol is 3; a backward-incompatible protocol designed for Python 3.
 61 
 62         Specifying a negative protocol version selects the highest protocol
 63         version supported.  The higher the protocol used, the more recent the
 64         version of Python needed to read the pickle produced.
 65 
 66         If *fix_imports* is True and *protocol* is less than 3, pickle will
 67         try to map the new Python 3 names to the old module names used in
 68         Python 2, so that the pickle data stream is readable with Python 2.
 69 
 70     load(file, *, fix_imports=True, encoding=‘ASCII‘, errors=‘strict‘)
 71         Read and return an object from the pickle data stored in a file.
 72 
 73         This is equivalent to ``Unpickler(file).load()``, but may be more
 74         efficient.
 75 
 76         The protocol version of the pickle is detected automatically, so no
 77         protocol argument is needed.  Bytes past the pickled object‘s
 78         representation are ignored.
 79 
 80         The argument *file* must have two methods, a read() method that takes
 81         an integer argument, and a readline() method that requires no
 82         arguments.  Both methods should return bytes.  Thus *file* can be a
 83         binary file object opened for reading, an io.BytesIO object, or any
 84         other custom object that meets this interface.
 85 
 86         Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
 87         which are used to control compatibility support for pickle stream
 88         generated by Python 2.  If *fix_imports* is True, pickle will try to
 89         map the old Python 2 names to the new names used in Python 3.  The
 90         *encoding* and *errors* tell pickle how to decode 8-bit string
 91         instances pickled by Python 2; these default to ‘ASCII‘ and ‘strict‘,
 92         respectively.  The *encoding* can be ‘bytes‘ to read these 8-bit
 93         string instances as bytes objects.
 94 
 95     loads(data, *, fix_imports=True, encoding=‘ASCII‘, errors=‘strict‘)
 96         Read and return an object from the given pickle data.
 97 
 98         The protocol version of the pickle is detected automatically, so no
 99         protocol argument is needed.  Bytes past the pickled object‘s
100         representation are ignored.
101 
102         Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
103         which are used to control compatibility support for pickle stream
104         generated by Python 2.  If *fix_imports* is True, pickle will try to
105         map the old Python 2 names to the new names used in Python 3.  The
106         *encoding* and *errors* tell pickle how to decode 8-bit string
107         instances pickled by Python 2; these default to ‘ASCII‘ and ‘strict‘,
108         respectively.  The *encoding* can be ‘bytes‘ to read these 8-bit
109         string instances as bytes objects.
110 """

 

以上是关于python pickle模块的主要内容,如果未能解决你的问题,请参考以下文章

python文件操作:pickle模块多次dump后出现的读取问题

Python:pickle模块学习

python基础(20):序列化json模块pickle模块

Python pickle模块

Python pickle模块

Python pickle模块