Open In App

marshal — Internal Python object serialization

Serializing a data means converting it into a string of bytes and later reconstructing it from such a string. If the data is composed entirely of fundamental Python objects, the fastest way to serialize the data is by using marshal module (For user defined classes, Pickle should be preferred). Marshal module contains functions that can read and write Python values in a binary format. The marshal module exists mainly to support reading and writing the “pseudo-compiled” code for Python modules of .pyc files. This module doesn’t support all Python object types. The following types are supported: booleans, integers, floating point numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets, frozensets, dictionaries, and code objects, where it should be understood that tuples, lists, sets, frozensets and dictionaries are only supported as long as the values contained therein are themselves supported. The singletons None, Ellipsis and StopIteration can also be marshalled and unmarshalled. Functions : 





Output

{tfeep[tctitatog®Gáz®ó?y@@ittwelve(iiiuwer0}




# Python code to demonstrate de-serialization
import marshal
 
data = {12:'twelve', 'feep':list('ciao'), 1.23:4+5j,
        (1,2,3):u'wer'}
bytes = marshal.dumps(data)
redata = marshal.loads(bytes)
 
print (redata)

Output

{12: 'twelve', 1.23: (4+5j), 'feep': ['c', 'i', 'a', 'o'], (1, 2, 3): u'wer'}
Article Tags :