Python USES the marshal module to serialize instances

  • 2020-04-02 14:11:32
  • OfStack

This article illustrates the method of using marshal module serialization in python. Specific methods are as follows:

Take a look at the following code:


import marshal
data1 = ['abc',12,23,'jb51']  # Several test data 
data2 = {1:'aaa',"b":'dad'}
data3 = (1,2,4)

output_file = open("a.txt",'wb')# Serialize the data to a file. 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')# Reads serialized data from a file 
#data1 = []
data1 = marshal.load(input_file)
data2 = marshal.load(input_file)
data3 = marshal.load(input_file)
print data1# Print out the results for the comrades 
print data2
print data3


outstring = marshal.dumps(data1)#marshal.dumps() The 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', 12, 23, 'jb51']
{1: 'aaa', 'b': 'dad'}
(1, 2, 4)
['abc', 12, 23, 'jb51']

The several functions of the marshel module are officially described as follows:

The module defines these functions:
Marshal. Dump (value, the file [, version])
Write the value on the open file. The value must be a supported type. The file must be an open file 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 read back by load().
New in version 2.4: The version argument indicates The data format that dump should use (see below).
Marshal. The 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 marshalled with dump(), load() will substitute None for the unmarshallable type.
Marshal. Dumps (value [version])
Return the string that would be written to a file by dump(value, File). The value must be a supported type. Raise a ValueError exception if The 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 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 are ignored.
In addition, the following constants are defined:
Marshal. Version
Indicates the format that the module USES.

Marshal. The usefulness of the version : marshal does not guarantee compatibility between different python versions, so it keeps functions with different version information.

I hope that this article has helped you to learn Python programming.


Related articles: