Example analysis of Python pickle module usage

Source: Internet
Author: User
Tags serialization

This example describes the use of Python pickle modules. Share to everyone for your reference. The specific analysis is as follows:

Pickle provides a simple persistence function. objects can be stored on disk as files.

Pickle.dump (obj, file[, Protocol])

Serializes the object and writes the resulting data stream to the file object. 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 indicates serialization in binary form.

Pickle.load (file)

Deserializes the object. Resolves the data in a file to a Python object.

It should be noted that at load (file), to allow Python to find the definition of the class, it will be an error:

Like the following example

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 Import Pickle class Person:def __init__ (self,n,a): Self.name=n self.age=a def show (self): print self.name+ "_" +str (SELF.A GE) AA = person ("Jgood", 2) Aa.show () f=open (' D:p.txt ', ' W ') Pickle.dump (aa,f,0) f.close () #del person F=open (' d:p.txt ', ' r ') Bb=pickle.load (f) f.close () bb.show ()

If you do not comment out del person, then the error will be as follows:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 >>> jgood_2 Traceback (most recent): File "c:/py/test.py", line, in <module> bb=pickle.load ( f) file "c:python27libpickle.py", line 1378, in Load return Unpickler (file). Load () file "c:python27libpickle.py", line 858 , in Load Dispatch[key] (self) File "c:python27libpickle.py", line 1069, in Load_inst klass = Self.find_class (module, name) File "c:python27libpickle.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 Pickler's "memo". Using the Pickler instance, when serializing an object, it "remembers" the object reference that has been serialized, so that dump (obj) is called multiple times on the same object, and Pickler is not "goofy" to serialize multiple times.

Look at the following example:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 Import Stringio Import Pickle class 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:

?

1 2 3 4 5 >>> Jgood_2 132 >>>

When you comment out the Pick.clear_memo (), the results are as follows:

?

1 2 3 4 5 >>> jgood_2 >>>

The main reason is that Python's pickle, if not clear_memo, will not serialize the object multiple times.

I hope this article will help you with your 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.