Python pickle Module

Source: Internet
Author: User
Tags serialization

1 ImportPickle2 3 #in Python, if we have some objects that require persistent storage and do not lose the type and data of our object,4 #we will need to serialize these objects, after serialization, we need to use the time when we reply to the original data,5 #serialization of this process, we call it pickle (pickled)6 7 8 #1. Dumps (object) Python Object----string9List_a = ["Mingyue","Jishi"," You"]TenList_b =pickle.dumps (list_a) One Print(List_b) A  -  - #2, loads (string)----Python object theList_c =pickle.loads (list_b) - Print(List_c) -  -  + #3. Dump (object, file) Python Object--file -Group1 = ("Baidu","Wen","Qingtian") +F1 = open ("1.PK1","WB") A pickle.dump (group1, F1, True) at f1.close () -  - #4. Load (object, file) ----Python object -F2 = open ("1.PK1","RB") -t =pickle.load (F2) - Print(t) in f2.close () -  to """ + dump (obj, file, Protocol=none, *, fix_imports=true) - Write a pickled representation of obj to the open file object file. the  * This is equivalent to ' Pickler (file, protocol). Dump (obj) ", but may $ Be more efficient.Panax Notoginseng  - The optional *protocol* argument tells the Pickler to use the given the protocol supported protocols is 0, 1, 2, 3 and 4. The default + protocol is 3, a backward-incompatible protocol designed for Python 3. A  the 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 has a write () method that accepts a single - bytes argument. It can thus is a file object opened for binary - writing, an IO. Bytesio instance, or any other custom object that meets the This interface. - Wuyi If *fix_imports* is True and protocol are less than 3, Pickle would try the To map the new Python 3 names to the old module names used in Python - 2, so, the pickle data stream was readable with Python 2. Wu  - dumps (obj, Protocol=none, *, fix_imports=true) About 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 is 0, 1, 2, 3 and 4. The default - protocol is 3, a backward-incompatible protocol designed for Python 3. A  + specifying a negative protocol version selects the highest protocol the version supported. The higher the protocol used, the more recent the - version of Python needed to read the pickle produced. $  the If *fix_imports* is True and *protocol* are less than 3, pickle would the try to map the new Python 3 names to the old module names used in the python 2, so, the pickle data stream was readable with Python 2. the  - load (file, *, Fix_imports=true, encoding= ' ASCII ', errors= ' strict ') in Read and return an object from the pickle data stored in a file. the  the This is equivalent to ' Unpickler (file). Load () About efficient. the  the the protocol version of the pickle is detected automatically, so no the protocol argument is needed. Bytes past the Pickled object ' s + representation is ignored. -  the The argument *file* must has both methods, a read () method that takesBayi An integer argument, and a ReadLine () method that requires no the arguments.  Both methods should return bytes. Thus *file* can be a the binary file object opened for reading, an IO. Bytesio object, or any - Other custom Object the meets this interface. -  the Optional keyword arguments is *fix_imports*, *encoding* and *errors*, the which is used to control compatibility support for Pickle stream the generated by Python 2. If *fix_imports* is True, Pickle would try to the map the old Python 2 names to the new names used in Python 3. the - *encoding* and *errors* tell Pickle what to decode 8-bit string the instances pickled by Python 2; These default to ' ASCII ' and ' strict ', the respectively. The *encoding* can is ' bytes ' to read these 8-bit the string instances as bytes objects.94  the loads (data, *, Fix_imports=true, encoding= ' ASCII ', errors= ' strict ') the Read and return an object from the given pickle data. the 98 the protocol version of the pickle is detected automatically, so no About protocol argument is needed. Bytes past the Pickled object ' s - representation is ignored.101 102 Optional keyword arguments is *fix_imports*, *encoding* and *errors*,103 which is used to control compatibility support for Pickle stream104 generated by Python 2. If *fix_imports* is True, Pickle would try to the map the old Python 2 names to the new names used in Python 3. the106 *encoding* and *errors* tell Pickle what to decode 8-bit string107 instances pickled by Python 2; These default to ' ASCII ' and ' strict ',108 respectively. The *encoding* can is ' bytes ' to read these 8-bit109 string instances as bytes objects. the """

Python pickle Module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.