save a dictionary in a file
Josiah Carlson
jcarlson at uci.edu
Mon Nov 15 20:58:41 EST 2004
Jeff Shannon <jeff at ccvcorp.com> wrote:
>
> Josiah Carlson wrote:
>
> >Jeff Shannon <jeff at ccvcorp.com> wrote:
> >
> >
> >>Luis P. Mendes wrote:
> >>
> >>
> >>
> >>>my program builds a dictionary that I would like to save in a file.
> >>>
> >>>
> >>As others have said, there should be no reason to convert to a list
> >>before pickling -- AFAIK, there's no ban against pickling dictionaries.
> >>
> >>You might also look into the Shelve module, which implements a
> >>filesystem-persisting object with dictionary access syntax. (And which,
> >>IIRC, uses pickled dictionaries under the covers.)
> >>
> >>
> >
> >It doesn't. It uses a bsddb database.
> >
> >A bsddb (really anydbm which can give you bsddb) database is merely a
> >string key->string value mapping (for btree and hash mappings). You
> >access it via string keys, and its values are automatically translated
> >to/from strings via Pickle. The only way you actually pickle and
> >unpickle dictionaries is by having a dictionary in a value.
> >
> >
>
> Ah, my mistake. (I've never really had call to use shelve *or* pickle,
> and was going off of (apparently erroneous) memories of conversations in
> c.l.py.)
>
> Still, it does seem that shelve would probably fit the O.P.'s needs...
It depends on how much reading and writing to/from the dictionary is
done. For many reads, a caching shelve instance works well, but ends up
creating an in-memory copy anyways. When writing, one needs to be
careful of the number of writes and how long it takes to update the
shelve instance, and whether one wants a bunch of writes to the instance
done during close.
- Josiah
More information about the Python-list
mailing list