
- Python Data Persistence Tutorial
- Python Data Persistence - Home
- Python Data Persistence - Introduction
- Python Data Persistence - File API
- File Handling with os Module
- Python Data Persistence - Object Serialization
- Python Data Persistence - Pickle Module
- Python Data Persistence - Marshal Module
- Python Data Persistence - Shelve Module
- Python Data Persistence - dbm Package
- Python Data Persistence - CSV Module
- Python Data Persistence - JSON Module
- Python Data Persistence - XML Parsers
- Python Data Persistence - Plistlib Module
- Python Data Persistence - Sqlite3 Module
- Python Data Persistence - SQLAlchemy
- Python Data Persistence - PyMongo module
- Python Data Persistence - Cassandra Driver
- Data Persistence - ZODB
- Data Persistence - Openpyxl Module
- Python Data Persistence Resources
- Python Data Persistence - Quick Guide
- Python Data Persistence - Useful Resources
- Python Data Persistence - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Data Persistence - Marshal Module
Object serialization features of marshal module in Python’s standard library are similar to pickle module. However, this module is not used for general purpose data. On the other hand, it is used by Python itself for Python’s internal object serialization to support read/write operations on compiled versions of Python modules (.pyc files).
The data format used by marshal module is not compatible across Python versions. Therefore, a compiled Python script (.pyc file) of one version most probably won’t execute on another.
Just as pickle module, marshal module also defined load() and dump() functions for reading and writing marshalled objects from / to file.
dump()
This function writes byte representation of supported Python object to a file. The file itself be a binary file with write permission
load()
This function reads the byte data from a binary file and converts it to Python object.
Following example demonstrates use of dump() and load() functions to handle code objects of Python, which are used to store precompiled Python modules.
The code uses built-in compile() function to build a code object out of a source string which embeds Python instructions.
compile(source, file, mode)
The file parameter should be the file from which the code was read. If it wasn’t read from a file pass any arbitrary string.
The mode parameter is ‘exec’ if the source contains sequence of statements, ‘eval’ if there is a single expression or ‘single’ if it contains a single interactive statement.
The compile code object is then stored in a .pyc file using dump() function.
import marshal script = """ a=10 b=20 print ('addition=',a+b) """ code = compile(script, "script", "exec") f=open("a.pyc","wb") marshal.dump(code, f) f.close()
To deserialize, the object from .pyc file use load() function. Since, it returns a code object, it can be run using exec(), another built-in function.
import marshal f=open("a.pyc","rb") data=marshal.load(f) exec (data)