Document directory
- Marshal. Dump (value, file [, Version])
- Marshal. Load (file)
- Marshal. dumps (value [, Version)
- Marsahl. Load (string)
Sometimes, you need to save an object in the memory to the disk persistently, or serialize the object to a binary stream over the network and send it to a remote host. Many modules in Python provide serialization and deserialization functions, such as marshal, pickle, and cpickle. Let's talk about the marshal module today.
Note:
Marshal is not a common module. In some cases, it is not recommended to be used because the binary data format serialized using marshal is not documented, in python of different versions, the implementation of Marshal may be different. That is to say, using the python sequence as an object and deserializing the object with the python2.6 program may be different from the original object. But the meaning of this module, as described in the python Manual:Marshal
Module exists mainly to support reading and writing the "pseudo-compiled" code for python modules. PyC
Files.
The following are some functions related to serialization/deserialization defined in the marshal module:
Marshal. Dump (value, file [, Version])
Write the value to an open output stream. Parameter value indicates the value to be serialized. File indicates the Open output stream. For example, a file opened in "WB" mode is sys. stdout or OS. popen. For some types that do not support sequence classes, the dump method will throw a valueerror exception. Note that 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, dict, and code objects. For collection objects such as tuple, list, set, and dict, the elements must also be one of the above types.
Marshal. Load (file)
Execute the operation opposite to marshal. Dump and column binary data in reverse order as a python object. The following is an example to demonstrate the use of the two methods:
1 |
# Coding = GBK |
2 |
|
3 |
Import
Marshal , Sys
, OS
|
4 |
|
5 |
LST = [ 1
, ( 2
, " String " ) , { " Key " : " Value " } ] |
6 |
|
7 |
# Serialize to a file |
8 |
FLE = Open
( OS
. Path . Join ( OS
. Getcwd ( ) , ' FLE . Txt ' ) , ' WB ' ) |
9 |
Marshal . Dump ( LST , FLE ) |
10 |
FLE . Close ( ) |
11 |
|
12 |
# Deserialization |
13 |
Fle1 = Open
( OS
. Path . Join ( OS
. Getcwd ( ) , ' FLE . Txt ' ) , ' RB ' ) |
14 |
Lst1 = Marshal . Load ( Fle1 ) |
15 |
Fle1 . Close ( ) |
16 |
|
17 |
# Print results |
18 |
Print
LST |
19 |
Print
Lst1 |
20 |
|
21 |
# ---- Result ---- |
22 |
# [1, (2, 'String '), {'Key ': 'Value'}] |
23 |
# [1, (2, 'String '), {'Key ': 'Value'}] |
Marshal. dumps (value [, Version)
This method is similar to the marshal. Dump () function mentioned above, but it returns a serialized binary stream instead of directly writing the data into the file.
Marsahl. Load (string)
Deserializes binary streams into objects. The following code demonstrates the use of the two methods:
1 |
Import
Marshal , Sys
, OS
|
2 |
|
3 |
LST = [ 1
, ( 2
, " String " ) , { " Key " : " Value " } ] |
4 |
|
5 |
Byt1 = Marshal . Dumps ( LST ) |
6 |
Lst1 = Marshal . Loads ( Byt1 ) |
7 |
|
8 |
# Print results |
9 |
Print
LST |
10 |
Print
Lst1 |
11 |
|
12 |
# ---- Result ---- |
13 |
# [1, (2, 'String '), {'Key ': 'Value'}] |
14 |
# [1, (2, 'String '), {'Key ': 'Value'}] |
|
|
For more information about marshal, see the python manual.