Python uses the marshal module to serialize instances

Source: Internet
Author: User
Tags unsupported
The examples in this paper describe how Python uses the Marshal module serialization method and share it for everyone's reference. Here's how:

Let's take a look at the following code:

Import marshaldata1 = [' abc ', 12,23, ' jb51 ']  #几个测试数据data2 = {1: ' aaa ', ' B ': ' Dad '}data3 = (1,2,4) output_file = Open (" A.txt ", ' WB ') #把这些数据序列化到文件中, note: The file must be opened in binary mode Marshal.dump (data1,output_file) marshal.dump (data2,output_file) Marshal.dump (Data3,output_file) output_file.close () input_file = open (' a.txt ', ' RB ') #从文件中读取序列化的数据 #data1 = []DATA1 = Marshal.load (input_file) data2 = Marshal.load (input_file) data3 = Marshal.load (input_file) Print data1# Print the results to the Comrades see print Data2print data3outstring = Marshal.dumps (data1) #marshal. The dumps () return is a byte string that is used to write to the file open (' OUT.txt ' , ' WB '). Write (outstring) file_data = open (' OUT.txt ', ' RB '). Read () Real_data = Marshal.loads (file_data) Print Real_data

Results:

[' abc ', ' A ', ' jb51 '] {1: ' aaa ', ' B ': ' Dad '} (1, 2, 4) [' abc ', ' A ', ' jb51 ']

Several functions of the Marshel module are officially described as follows:

The module defines these functions:
Marshal.dump (value, file[, Version])
Write the value on the open file. The value must is a supported type. The file must be a open file object such as Sys.stdout or returned by open () or Os.popen (). It must is opened in binary mode (' WB ' or ' w+b ').
If the value has (or contains an object) a unsupported type, a ValueError exception is raised-but garbage data Would also is written to the file. The object is not being properly read back by load ().
New in version 2.4:the version argument indicates the data format this dump should 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 Eofe Rror, ValueError or TypeError. The file must is an open file object opened in binary mode (' RB ' or ' r+b ').
Warning
If an object containing an unsupported type is marshalled with dump (), load () would substitute None for the unmarshallable Type.
Marshal.dumps (value[, Version])
Return the string that would is written to a file by dump (value, file). The value must is a supported type. Raise a ValueError exception if value has (or contains an object, a) an unsupported type.
New in version 2.4:the version argument indicates the data format this dumps should 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 is ignored.
In addition, the following constants is defined:
Marshal.version
Indicates the format that the module uses.

use of marshal.version :Marshal does not guarantee compatibility between different versions of Python, so it retains a function of version information.

I hope this article is helpful to the study of Python program design.

  • 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.