marshal — Internal Python object serialization - GeeksforGeeks

01 Feb, 2022
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 :
marshal.version :It indicates the format used by the module.Version 0 – Historical formatVersion 1 – Shares interned stringsVersion 2 – Uses a binary format for floating point numbersVersion 3 – Support for object instancing and recursionVersion 4 – Current Versionmarshal.dumps(value[, version]) :The function returns the bytes object that would be written to a file by dump(value, file). The version argument indicates the data format that dumps should use. A ValueError exception is raised if the value has (or contains an object that has) an unsupported type.ExamplePython3Python3# Python code to demonstrate serializationimport marshal  data = {12:'twelve', 'feep':list('ciao'), 1.23:4+5j,         (1,2,3):u'wer'}bytes = marshal.dumps(data)  print (bytes)Output{tfeep[tctitatog®Gáz®ó?y@@ittwelve(iiiuwer0}
marshal.loads(bytes) :This function can reconstruct the data by converting the bytes-like object to a value. EOFError, ValueError or TypeError is raised if no value is found.ExamplePython3Python3# Python code to demonstrate de-serializationimport 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'}
marshal.dump(value, file[, version]) :This function is used to write the supported type value on the open writeable binary file. A ValueError exception is raised if the value has an unsupported type.marshal.load(file) :This function reads one value from the open readable binary file and returns it. EOFError, ValueError or TypeError is raised if no value is read.
marshal.version :It indicates the format used by the module.Version 0 – Historical formatVersion 1 – Shares interned stringsVersion 2 – Uses a binary format for floating point numbersVersion 3 – Support for object instancing and recursionVersion 4 – Current Version
Version 0 – Historical format
Version 1 – Shares interned strings
Version 2 – Uses a binary format for floating point numbers
Version 3 – Support for object instancing and recursion
Version 4 – Current Version
marshal.dumps(value[, version]) :The function returns the bytes object that would be written to a file by dump(value, file). The version argument indicates the data format that dumps should use. A ValueError exception is raised if the value has (or contains an object that has) an unsupported type.ExamplePython3Python3# Python code to demonstrate serializationimport marshal  data = {12:'twelve', 'feep':list('ciao'), 1.23:4+5j,         (1,2,3):u'wer'}bytes = marshal.dumps(data)  print (bytes)Output{tfeep[tctitatog®Gáz®ó?y@@ittwelve(iiiuwer0}

Example
Python3
# Python code to demonstrate serializationimport marshal  data = {12:'twelve', 'feep':list('ciao'), 1.23:4+5j,         (1,2,3):u'wer'}bytes = marshal.dumps(data)  print (bytes)
Output
{tfeep[tctitatog®Gáz®ó?y@@ittwelve(iiiuwer0}

marshal.loads(bytes) :This function can reconstruct the data by converting the bytes-like object to a value. EOFError, ValueError or TypeError is raised if no value is found.ExamplePython3Python3# Python code to demonstrate de-serializationimport 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'}

Example
Python3
# Python code to demonstrate de-serializationimport 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)
{12: 'twelve', 1.23: (4+5j), 'feep': ['c', 'i', 'a', 'o'], (1, 2, 3): u'wer'}

marshal.dump(value, file[, version]) :This function is used to write the supported type value on the open writeable binary file. A ValueError exception is raised if the value has an unsupported type.
marshal.load(file) :This function reads one value from the open readable binary file and returns it. EOFError, ValueError or TypeError is raised if no value is read.
This article is contributed by Aditi Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
sagar0719kumar
gabaa406
prachisoda1234
Python-Library
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()