Python Pickle Module Usage Example analysis

Source: Internet
Author: User
This example describes the Python pickle module usage. Share to everyone for your reference. The specific analysis is as follows:

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

Import Pickleclass Person:  def __init__ (self,n,a):    self.name=n    self.age=a  def Show (self):    Print self.name+ "_" +str (self.age) AA = person ("Jgood", 2) Aa.show () f=open (' D:\\p.txt ', ' W ') Pickle.dump (aa,f,0) f.close () #del personf=open (' D:\\p.txt ', ' R ') bb=pickle.load (f) f.close () bb.show ()

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

>>> jgood_2 Traceback (most recent): File ' c:/py/test.py ', line, <module>  Bb=pickle.loa D (f) file "C:\Python27\lib\pickle.py", line 1378, in load  return Unpickler (file). Load () file "C:\Python27\lib\ pickle.py ", line 858, in Load  Dispatch[key] (self) File ' C:\Python27\lib\pickle.py ', line 1069, in Load_inst  Klass = Self.find_class (module, name) File "C:\Python27\lib\pickle.py", line 1126, in find_class  klass = getattr (mod, Name) Attributeerror: ' Module ' object has no attribute ' person '

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:

Import stringioimport pickleclass Person:  def __init__ (self,n,a):    self.name=n    self.age=a  def show ( Self):    print self.name+ "_" +str (self.age) AA = person ("Jgood", 2) aa.show () fle = Stringio.stringio () pick = pickle. Pickler (fle) pick.dump (AA) Val1=fle.getvalue () Print len (val1) Pick.clear_memo () pick.dump (AA) Val2=fle.getvalue () Print Len (val2) Fle.close ()

The above code runs as follows:

>>>JGood_266132>>>

After commenting out Pick.clear_memo (), the result is as follows:

>>>JGood_26670>>>

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

Hopefully this article will help you with Python programming.


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.