Here are a couple of snippets to get you started using cPickle. First import cPickle. The cPickle module should be included in most Python distributions
import cPickleIn this example, we have an array called "large_array" created by the "create_large_array()" function. I want to store "large_array" in a file called "large_array.cpickle".
large_array = create_large_array() output_filename = "large_array.cpickle" f = open(output_filename,"wb") cPickle.dump(large_array, f, protocol=2) f.close()In this example "open()" is called with the second argument "wb", which tells Python to open the file in write/binary mode. "cPickle.dump()" dumps "large_array" into "f" using "protocol=2". cPickle has several different modes which all do the same -- protocol 2 is the fastest.
If you want to load the array from "large_array.cpickle" in another script, you can use something like this:
def load_pickle(filename): f = open(filename,"rb") p = cPickle.load(f) f.close() return(p) large_array = load_pickle("large_array.cpickle")
It's also great for preparing complicated, non-human writable input files as used for the BioFET-SIM command line version.
ReplyDeleteThere is a typo in your first import.
ReplyDeleteThanks! I fixed the typo (as well as a couple of other typos).
ReplyDelete