The marshal Module
The marshal module is used to serialize data—that is, convert data to and
from character strings, so that they can be stored on file or sent
over a network. Example 4-9 illustrates this.
The marshal module uses a simple self-describing data format. For each data
item, the marshalled string contains a type code, followed by one or
more type-specific fields. Integers are stored in little-endian
order, strings are stored as length fields followed by the strings’ contents
(which can include null bytes), tuples are stored as length fields followed by
the objects that make up each tuple, etc.
Example 4-9. Using the marshal Module to Serialize Data
File: marshal-example-1.py
import marshal
value = (
"this is a string",
[1, 2, 3, 4],
("more tuples", 1.0, 2.3, 4.5),
"this is yet another string"
)
data = marshal.dumps(value)
# intermediate format
print type(data), len(data)
print "-"*50
print repr(data)
print "-"*50
print marshal.loads(data)
<type 'string'> 118
--------------------------------------------------
'(\004\000\000\000s\020\000\000\000this is a string
[\004\000\000\000i\001\000\000\000i\002\000\000\000
i\003\000\000\000i\004\000\000\000(\004\000\000\000
s\013\000\000\000more tuplesf\0031.0f\0032.3f\0034.
5s\032\000\000\000this is yet another string'
--------------------------------------------------
('this is a string', [1, 2, 3, 4], ('more tuples',
1.0, 2.3, 4.5), 'this is yet another string')The marshal module can also handle code objects (it’s used to store ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access