I had a couple of Python lists that I needed to use in another script on another computer. The annoying way would be to write out to a tab-delimited file, but luckily the shelve module (standard with Python), makes things much easier. It’s the equivalent of Matlab’s .mat files, where you store variables for later use. Here’s how to use it:
First, the ugly way
To show how useful the shelve module is, think for a moment what it would take to save variables as tab-delimited text files:
- write each list out to a tab-delimited text file
- copy each list’s text file to the other computer
- for each list, create an empty list
- open its file (a way of automatically knowing the filenames would be nice — sequential numbering perhaps?
- append each line to the new list
Ugh! Now what if I wanted to use two lists and a dictionary on another computer? That would get pretty annoying, pretty quickly.
A better way
Instead, I used the shelve module, which comes standard with Python. The way it works is almost self-explanatory (though maybe I’d call it a cupboard rather than a shelf since you open and close it).
Add stuff to be transported
import shelve
x = shelve.open('my_shelf.dat')
# Add stuff to the shelve object
x['first list'] = list1
x['second list'] = list2
x['a dictionary'] = dictionary1
# close it when you're done.
x.close()
As you can see, the shelve object acts much like a dictionary. It can store arbitrary objects, too. But not that it’s in there, how do you use it?
Use the stuff you added
To use the shelve object you just created, say, on another computer, just copy that single file over (in this case, my_shelf.dat). Then on that other computer, open it up and ask for what was in it. Like so:
import shelve
y = shelve.open('my_shelf.dat')
list_a = y['first list']
list_b = y['second list']
dict_1 = y['a dictionary']
y.close()
# insert code here that uses list_a, list_b, or dict_1 . . .
So the shelve object acts almost like a flat database.
“The way it works is almost self-explanatory (though maybe I’d call it a cupboard rather than a shelf since you open and close it).”
You can more about this?