Python pickle module usage Example Analysis

Source: Internet
Author: User

Python pickle module usage Example Analysis

This example describes how to use the Python pickle module. Share it with you for your reference. The specific analysis is as follows:

Pickle provides a simple persistence function. Objects can be stored on disks as files.

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

Serialize the object and write the result data stream to the file object. The protocol parameter is a serialization mode. The default value is 0, indicating serialization in text format. The value of protocol can also be 1 or 2, indicating that the protocol is serialized in binary format.

Pickle. load (file)

Deserialization object. Parses the data in the file into a Python object.

Note that when loading (file), python should be able to find the class definition; otherwise, an error will be reported:

For 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, ):

Self. name = n

Self. age =

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 Person

F = open ('d: p.txt ', 'R ')

Bb = pickle. load (f)

F. close ()

Bb. show ()

If del Person is not commented out, the following error is returned:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

>>>

JGood_2

 

Traceback (most recent call last ):

File "C:/py/test. py", line 15, 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 class definition cannot be found in the current module.

Clear_memo ()

Empty pickler's "Memo ". When using the Pickler instance to serialize an object, it will "remember" The object that has been serialized is referenced, so the same object is called multiple times dump (obj ), pickler won't perform multiple serialization as silly.

See 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, ):

Self. name = n

Self. age =

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

66

132

>>>

After pick. clear_memo () is commented out, the running result is as follows:

?

1

2

3

4

5

>>>

JGood_2

66

70

>>>

The main reason is that python pickle does not serialize objects multiple times without clear_memo.

I hope this article will help you with Python programming.

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.