Change language

Marshal — internal serialization of Python objects

| | |

The Marshal module exists primarily to support reading and writing "pseudo-compiled" code for Python modules in .pyc files. This module does not support all types of Python objects.

The following types are supported: booleans, integers, floating point numbers, complex numbers, strings, bytes, byte arrays, tuples, lists, sets, freezes, dictionaries and code objects, where it should be understood that tuples, lists, sets, freezes, and dictionaries are supported only as long as the values ​​they contain are supported. The singletones None, Ellipsis and StopItate can also be marshaled and parsed.

Functions :

  1. Marshal.version:
    Specifies the format used by the module.
    • Version 0 — Historical format
    • Version 1 — strips interned strings
    • Version 2 — uses binary format for floating point numbers
    • Version 3 — Object Instance and Recursion Support
    • Version 4 — Current version
  2. marshal.dumps (value [, version]):
    The function returns a byte object that would have been written to the file by dump (value, file). The version argument specifies the data format that the dump should use. A ValueError is thrown if the value is (or contains an object that has) an unsupported type.

    example

    # Python code to demonstrate serialization

    import marshal

     

    data = { 12 : ’twelve’ , ’feep’ : list ( ’ciao’ ), 1.23 : 4 + 5j

    ( 1 , 2 , 3 ): u ’wer’ }

    bytes = marshal.dumps (data)

     

    print bytes

    Exit

     {tfeep [tctitatog®Gáz®ó? y @@ ittwelve (iiiuwer0} 
  3. marshal.loads (bytes):
    This function can reverse engineer data by converting a byte-like object to a value. EOFError, ValueError, or TypeError are raised if no value is found.

    example

    # Python code to demonstrate deserialization

    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

    Exit

     {12: ’twelve’, 1.23: (4 + 5j ), ’feep’: [’ c’, ’i’,’ a’, ’o’], (1, 2, 3): u’wer’} 
  4. marshal.dump (value, file [, version]):
    This function is used to write a value of a supported type to an open writeable binary f isle. A ValueError exception is thrown if the value is of an unsupported type.
  5. marshal.load (file):
    This function reads one value from an open readable binary file and returns it. EOFError, ValueError, or TypeError are raised if the value is not readable.

This article is provided by Aditi Gupta . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically