views:

294

answers:

2

What is the most efficient method to store a Python dictionary on the disk? The only methods I know of right now are plain-text and the pickle module.

Edit: Sorry for not being very clear. By efficient I meant fastest execution speed. The dictionary will contain mutable objects that will hold information to be parsed and modified.

+6  A: 

shelve is pretty nice as well

or this persistent dictionary recipe

for a convenient method that keeps your objects synchronized with storage, there's the ORM SQLAlchemy for python

if you just need a way to store string values by some key, theres the dbm.ndbm and dbm.gnu modules.

if you need a hyper efficient, distributed key value cache, something like memcached for python...

jspcal
`shelve` is usually all you'll need, unless you need super-fast performance, in which case you should be using an actual database anyway.
musicfreak
Does shelve use cPickle when possible?
linkmaster03
it indeed uses cPickle if available, you could override it by setting shelve.Pickler as well
jspcal
Thanks. I implemented my dictionary using shelve and it works great.
linkmaster03
+2  A: 

JSON and YAML work well, also.

Depends on what you mean by "efficient"? Size of file? Time required? Amount of code you need to write?

You have the timeit module available to determine what meets your specific criteria for "efficient".

S.Lott
Execution speed.
linkmaster03
@linkmaster03: Please update your question with additional, useful facts. Hiding important information on comments on an answer doesn't help anyone understand your question.
S.Lott