Python, Pickle

Source: Internet
Author: User

@Python Pickle Module Learning

The pickle provides a simple persistence feature. You can store the object as a file on disk.

------------------------------------------

Pickle.dump (obj, file[, Protocol])
Serializes the object and writes the resulting data stream to the file object. The parameter protocol is a serialization mode with a default value of 0, which indicates serialization as text. The value of the protocol can also be 1 or 2, which is serialized as a binary representation.

------------------------------------------
Pickle.load (file)
Deserializes the object. Resolves the data in a file to a Python object.

It is important to note that in the case of load (file), Python is able to find the definition of the class, otherwise it will give an error:

such as the following example

[Python]View Plaincopy
  1. Import Pickle
  2. Class Person:
  3. def __init__ (self,n,a):
  4. self.name=n
  5. self.age=a
  6. def show (self):
  7. print self.name+"_" +str (self.age)
  8. AA = Person ("Jgood", 2)
  9. Aa.show ()
  10. F=open (' d:\\p.txt ',' W ')
  11. Pickle.dump (Aa,f,0)
  12. F.close ()
  13. #del person
  14. F=open (' d:\\p.txt ',' R ')
  15. Bb=pickle.load (f)
  16. F.close ()
  17. Bb.show ()

If the Del person is not commented out, then the error is as follows:

This means that the current module cannot find the definition of the class.

--------------------------------------------------

Clear_memo ()
Empty the Pickler "Memo". When serializing an object using an Pickler instance, it "remembers" the object reference that has already been serialized, so the dump (obj) is called multiple times for the same object, and Pickler is not "silly" to serialize multiple times.
Look at the following example:

[Python]View Plaincopy
  1. Import Stringio
  2. Import Pickle
  3. Class Person:
  4. def __init__ (self,n,a):
  5. self.name=n
  6. self.age=a
  7. def show (self):
  8. print self.name+"_" +str (self.age)
  9. AA = Person ("Jgood", 2)
  10. Aa.show ()
  11. Fle = Stringio.stringio ()
  12. Pick = pickle. Pickler (fle)
  13. Pick.dump (AA)
  14. Val1=fle.getvalue ()
  15. Print Len (val1)
  16. Pick.clear_memo ()
  17. Pick.dump (AA)
  18. Val2=fle.getvalue ()
  19. Print Len (val2)
  20. Fle.close ()

The above code runs as follows:

If it is not commented out, the result of the run is the first one. If commented out, the result of the run is the second one.

The main reason is that Python's pickle, if not clear_memo, will not serialize the object more than once.

Python, Pickle

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.