Sometimes, an object in memory is persisted to disk, or serialized into a binary stream that is sent over the network to the remote host. There are many modules in Python that provide functionality for serialization and deserialization, such as: Marshal, Pickle, cpickle, and so on. Talk about the Marshal module today.
- Note: Marshal is not a generic module, and at some point it is not a recommended module because binary data formats that use marshal serialization are not documented, and in different versions of Python, the implementation of Marshal may not be the same. In other words, using the python2.5 sequence as an object, the python2.6 program deserializes the resulting object, which may not be the same as the original object. But the meaning of this module exists, as the Python handbook says: The marshal module exists mainly to support reading and writing the "pseudo-compiled" code for Py Thon Modules of PYC files.
The following are some of the functions defined in the Marshal module related to serialization/deserialization:
Marshal.dump (value, file[, Version])
Writes the value to an open output stream. The parameter value represents the value to be serialized. File represents an open output stream. Such as: Files opened in "WB" mode, Sys.stdout or Os.popen. For some types that do not support sequence classes, the Dump method throws a ValueError exception. In particular, not all types of objects can be serialized/deserialized using the Marshal module. In python2.6, the supported types are: None, integers, long integers, floating point numbers, strings, Unicode objects, tuple, list, set, dic T, and code objects. For tuple, list, set, Dict, and other collection objects, the elements must also be one of the above types.
Marshal.load (file)
Performs the opposite of Marshal.dump, deserializing the binary data into a Python object. Here is an example of how these two methods are used:
# CODING=GBK Import marshal, sys, os lst = [1, (2, "string"), {"Key": " Value "}] # serialized into file fle = open (OS. Path. Join (OS. GETCWD (), ' fle. txt '), ' WB ') Marshal. Dump (LST, fle) fle. Close () # deserialization fle1
= Open (OS. path. Join (OS. GETCWD (), ' fle. txt '), ' RB ') lst1 = Marshal. Load (Fle1) Fle1. Close () # Prints the result print lstprint lst1 #---- result ----# [1, (2, ' string '), {' key ': ' Value '}] # [1, (2, ' string '), {' key ': ' Value '}]marshal.dumps (value[, version)
This method is similar to the Marshal.dump () function above, except that it returns a serialized binary stream instead of writing the data directly to the file.
Marsahl.load (String)
Deserializes a binary stream into an object. The following section of code demonstrates the use of these two methods:
Import Marshal, sys, os lst = [1, (2, "string"), {"Key": "Value"}] B Yt1 = Marshal. Dumps (LST) lst1 = marshal. Loads (byt1) # Printing results print lstprint lst1 #--
results --# [1, (2, ' string '), {' key ': ' Value '}]# [1, (2, ' string '), {' Key ': ' Value '}]
For more information on Marshal, please refer to the Python manual.