Python uses the Marshal module to serialize the instance _python

Source: Internet
Author: User
Tags unsupported

This article describes the Python use of Marshal module serialization method, share for everyone to reference. The specific methods are as follows:

Let's take a look at the following code:

Import marshal
data1 = [' abc ', 12,23, ' jb51 ']  #几个测试数据
data2 = {1: ' aaa ', ' B ': ' Dad '}
data3 = (1,2,4)

output_file = open ("A.txt", "WB") #把这些数据序列化到文件中, note: 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# give the comrades the results to see print
data2 print data3


outstring = marshal.dumps (data1) #marshal. Dumps () returns 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 ', ' jb51 ']
{1: ' aaa ', ' B ': ' Dad '}
(1, 2, 4)
[' abc ', ' 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 be a supported type. The file must be a 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 so has) a unsupported type, a ValueError exception is raised-but garbage data Would also be written to the file. The object is not is properly read back by load ().
The New in version 2.4:the version argument indicates the data format of this dump should use.
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 be a 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 be a supported type. Raise a ValueError exception if value has (or contains an object so has) an unsupported type.
The New in version 2.4:the version argument indicates the "data format" dumps should use (.
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 versions of Python, so the function of preserving version information.

I hope this article will help you with your study of Python programming.

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.