marshal, Your Best Friend in the Python Universe
Who or what is marshal? It’s a built in Python library that lets you save Python’s built in data types to a file for later use. It’s very useful as a simple way to store things to disk and it’s much faster than the more general purpose Pickle library.
Here’s how you use it:
good_list=[1,2,3]
import marshal
marshal.dump(good_list, file('goodlist.p','wb'), 1)
#now close your program reopen it weeks later
import marshal
good_list=marshal.load(file('goodlist.p','rb'))
More information:
3.19 marshal — Internal Python object serialization
[tags]Python, Serialization, lists, marshal, Pickle, Programming[/tags]
marshal has two pretty big caveats: the serialization can change in any minor version of Python, and it only supports a limited set of primitive data structures.
The only time I’ve really seen marshal used intentionally is to manipulate .pyc files without going through the normal import code path (pyc files are just a marshal of a module).
Pickle on the other hand can serialize anything you teach it to, and the format is well specified and stays compatible across versions of Python.
I suggest you have a look at my optimization story when I discovered marshal as a “replacement” to cPickle:
http://www.peterbe.com/plog/python-optimization-anecdote
I prefer marshal over pickle because I’ve always gotten a huge speed boost from it. Pickle seems quite slow to me, and it seemed unusable for large data storage (yes, I even tried the proto option).
I think marshal calls are easier for me to type but I don’t remember pickle syntax offhand.