Home
Map
marshal Example (dump, load)Use the marshal module with a file to read and write values like lists, strings and integers.
Python
This page was last reviewed on Apr 3, 2024.
Marshal. Sometimes in Python we want to write many values to a file, and then read them from the file. When many things need to be written, performance is important.
With the marshal module, we can invoke the dump and load methods to write and read values. Marshal is simpler, and has fewer features than pickle.
pickle
Example. This Python program creates a file called "marshal_test" and writes a list, string and some integers to it. Then it opens the file again, and reads in those same values.
Step 1 We call marshal.dump to write a list of 20 values (created with a list comprehension) to the marshal_test file.
List Comprehension
Step 2 Marshal can handle strings. We write a string literal to the file with the dump() method.
Step 3 We write 5 integers to the file—usually, it is better to just use a list of integers, as the length can be stored within the file.
Step 4 Here we begin loading the data from the marshal_test file. We first load the list, and print it.
print
Step 5 We read the string from the file that we dumped earlier—the value "bird" is returned by load().
Step 6 We use marshal.load on the 5 integers. It is usually better to use lists, as the length of elements is included in the list data.
import marshal with open("marshal_test", "wb") as f: # Step 1: write a list to the file with marshal. values = [n for n in range(0, 20)] marshal.dump(values, f) # Step 2: write a string. marshal.dump("bird", f) # Step 3: write 5 integers. for v in range(0, 5): marshal.dump(v, f) with open("marshal_test", "rb") as f: # Step 4: load the list from the file. res = marshal.load(f) print(res) # Step 5: load the string from the file. word = marshal.load(f) print(word) # Step 6: load 5 integers. for v in range(0, 5): number = marshal.load(f) print(number)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] bird 0 1 2 3 4
The pickle module is recommended instead of marshal, but the marshal module can also be used to write and read values. Marshal (and pickle) are much faster than parsing text files.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 3, 2024 (new).
Home
Changes
© 2007-2024 Sam Allen.