Python serializes an instance using the marshal module

Source: Internet
Author: User
Tags unsupported
This article mainly introduces how to use the marshal module serialization in python, which is a very practical technique. if you need it, refer to the following example to describe how to use the marshal module serialization in python, share it with you for your reference. The specific method is as follows:

Let's take a look at the following code:

Import into aldata1 = ['ABC', 'jb51 '] # test data data2 = {1: 'AAA', "B": 'Dad'} data3 =, 4) output_file = open ("a.txt", 'wb') # serialize the data to a file. note: the file must be opened in binary mode. dump (data1, output_file) marshal. dump (data2, output_file) marshal. dump (data3, output_file) output_file.close () input_file = open('a.txt ', 'RB') # read serialized data from the file # data1 = [] data1 = marshal. load (input_file) data2 = marshal. load (input_file) data3 = marshal. load (input_file) print data1 # print the result to the comrades to see print data2print data3outstring = comment ', 'WB '). write (outstring) file_data = open('out.txt ', 'RB '). read () real_data = marshal. loads (file_data) print real_data

Result:

['abc', 12, 23, 'jb51']{1: 'aaa', 'b': 'dad'}(1, 2, 4)['abc', 12, 23, 'jb51']

Official descriptions of several functions of the El module are as follows:

The module defines these functions:
Marshal. dump (value, file [, version])
Write the value on the open file. the value must be a supported type. the file must be an open file object such as sys. stdout or returned by open () or OS. popen (). it must be opened in binary mode ('WB 'or 'W + B ').
If the value has (or contains an object that has) an unsupported type, a ValueError exception is raised-but garbage data will also be written to the file. the object will not be properly read back by load ().
New in version 2.4: The version argument indicates the data format that dump shocould use (see below ).
Marshal. load (file)
Read one value from the open file and return it. if no valid value is read (e.g. because the data has a different Python version's incompatible marshal format), raise EOFError, ValueError or TypeError. the file must be an open file object opened in binary mode ('RB' or 'r + B ').
Warning
If an object containing an unsupported type was has alled with dump (), load () will substitute None for the unmarshallable type.
Marshal. dumps (value [, version])
Return the string that wocould be written to a file by dump (value, file ). the value must be a supported type. raise a ValueError exception if value has (or contains an object that has) an unsupported type.
New in version 2.4: The version argument indicates the data format that dumps shocould use (see below ).
Marshal. loads (string)
Convert the string to a value. If no valid value is found, raise EOFError, ValueError or TypeError. Extra characters in the string are ignored.
In addition, the following constants are defined:
Marshal. version
Indicates the format that the module uses.

Use of marshal. version: Marshal does not guarantee compatibility between different python versions, so a function with version information is retained.

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