Knowledge of Marshal object serialization in Python

Source: Internet
Author: User
Tags manual join object serialization serialization in python

This article mainly introduces the Marshal object serialization in Python knowledge, is the Python Advanced Learning serialization related knowledge, need friends can refer to the

Sometimes, one object in memory is persisted to disk, or serialized into a binary stream sent over the network to a remote host. There are many modules in Python that provide serialization and deserialization capabilities, such as Marshal, Pickle, cpickle, and so on. Today we'll talk about the Marshal module.

Note: Marshal is not a general-purpose module, and at some point it is an deprecated module because binary data formats that are serialized using Marshal are not documented, and in different versions of Python, Marshal implementations may not be the same. In other words, using the python2.5 sequence as an object, using python2.6 program to deserialize the resulting object may not be the same as the original object. But the meaning of this module exists, as the Python manual says: The marshal module exists mainly to support reading and writing the "pseudo-compiled" code for Py Thon modules of. pyc files.

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

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

Writes a value to an open output stream. The parameter value represents the value to be serialized. File represents an open output stream. For example: File opened in "WB" mode, Sys.stdout or Os.popen. For some types that do not support sequence classes, the Dump method throws a ValueError exception. Specifically, 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, dic T, and code objects. For tuple, list, set, Dict and other collection objects, the elements must also be one of the above types.

Marshal.load (file)

Performs the opposite of the marshal.dump and deserializes the binary data into a Python object. The following is an example that demonstrates the use of these 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"}] # serialized to File Fle = Op En (OS. path. Join (OS. GETCWD (), ' fle. txt '), ' WB ') Marshal. Dump (LST, fle) fle. Close () # Deserialize fle1 = open (OS. path. Join (OS. GETCWD (), ' fle. txt '), ' RB ') Lst1 = Marshal. Load (fle1) fle1. Close () # Printing results 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 () feature mentioned above, except that it returns the serialized binary stream instead of writing the data directly to the file.

Marsahl.load (String)

Deserializes the binary stream into an object. The following section of code demonstrates the use of these 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) # prints results print LST print lst1 #--result-# [1, (2, ' string '), {' key ': ' Value '}] # [1, (2, ' string '), {' Ke Y ': ' Value '}]

For more information on Marshal, please refer to the Python manual.

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.