Knowledge about serialize Al Object serialization in Python

Source: Internet
Author: User
Tags object serialization

Knowledge about serialize Al Object serialization in Python

This article mainly introduces the serialization of marshal objects in Python. It is related to serialization in Python advanced learning. For more information, see

Sometimes, you need to save an object in the memory to the disk persistently, or serialize the object to a binary stream over the network and send it to a remote host. Many modules in Python provide serialization and deserialization functions, such as marshal, pickle, and cPickle. Let's talk about the marshal module today.

Note: marshal is not a common module. In some cases, it is not recommended because the binary data format serialized using marshal is not documented, in Python of different versions, the implementation of marshal may be different. That is to say, using the python sequence as an object and deserializing the object with the python2.6 program may be different from the original object. But The meaning of this module exists, as mentioned in the Python Manual: The specified Al module exists mainly to support reading and writing the "pseudo-compiled" code for Python modules of. pyc files.

The following are some functions related to serialization/deserialization defined in the marshal module:

Marshal. dump (value, file [, version])

Write the value to an open output stream. Parameter value indicates the value to be serialized. File indicates the Open output stream. For example, a file opened in "wb" mode is sys. stdout or OS. popen. For some types that do not support sequence classes, the dump method will throw a ValueError exception. Note that not all types of objects can be serialized/deserialized using the marshal module. In python2.6, the supported types include None, integers, long integers, floating point numbers, strings, Unicode objects, tuple, list, set, dict, and code objects. For collection objects such as tuple, list, set, and dict, the elements must also be one of the above types.

Marshal. load (file)

Execute the operation opposite to marshal. dump and column binary data in reverse order as a Python object. The following is an example to demonstrate the use of the two methods:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

# Coding = gbk

 

Import marshal, sys, OS

 

Lst = [1, (2, "string"), {"key": "Value"}]

 

# Serialize to a file

Fle = open (OS. path. join (OS. getcwd (), 'fle. txt '), 'wb ')

Marshal. dump (lst, fle)

Fle. close ()

 

# Deserialization

Fle1 = open (OS. path. join (OS. getcwd (), 'fle. txt '), 'rb ')

Lst1 = marshal. load (fle1)

Fle1. close ()

 

# Print the result

Print lst

Print lst1

 

# ---- Result ----

# [1, (2, 'string'), {'key': 'value'}]

# [1, (2, 'string'), {'key': 'value'}]

Marshal. dumps (value [, version)

This method is similar to the marshal. dump () function mentioned above, but it returns a serialized binary stream instead of directly writing the data into the file.

Marsahl. load (string)

Deserializes binary streams into objects. The following code demonstrates the use of the two methods:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Import marshal, sys, OS

 

Lst = [1, (2, "string"), {"key": "Value"}]

 

Byt1 = marshal. dumps (lst)

Lst1 = marshal. loads (byt1)

 

# Print the result

Print lst

Print lst1

 

# -- Result --

# [1, (2, 'string'), {'key': 'value'}]

# [1, (2, 'string'), {'key': 'value'}]

  

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.