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.
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.
marshal.dump
to write a list of 20 values (created with a list comprehension) to the marshal_test
file.string
literal to the file with the dump()
method.marshal_test
file. We first load the list, and print it.string
from the file that we dumped earlier—the value "bird" is returned by load()
.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.