Python pickle module, pythonpickle

Source: Internet
Author: User

Python pickle module, pythonpickle

1 import pickle 2 3 # in python, if we have some objects that require persistent storage and do not lose the types and data of our objects, 4 # We need to serialize these objects, after serialization, when we need to use it, we are replying to the original data, 5 # serialization of this process, we call it pickle (pickled) 6 7 8 #1. dumps (object) python object --> string 9 list_a = ["mingyue", "jishi", "you"] 10 list_ B = pickle. dumps (list_a) 11 print (list_ B) 12 13 14 #2. loads (string) string --> python object 15 list_c = pickle. loads (list_ B) 16 print (list_c) 17 18 19 #3. dump (object, file) python object --> file 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) file --> python object 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 shocould 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 '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 stream104 generated by Python 2. if * fix_imports * is True, pickle will try to105 map the old Python 2 names to the new names used in Python 3. the106 * encoding * and * errors * tell pickle how to decode 8-bit string107 instances pickled by Python 2; these default to 'ascii 'and 'strict', 108 respectively. the * encoding * can be 'bytes 'to read these 8-bit109 string instances as bytes objects.110 """

 

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.