Dictionary to string and back to dictionary??

Mark McEahern marklists at mceahern.com
Fri Aug 23 21:18:38 CEST 2002


> I have a set of records stored as dictionary objects.
> The records must be converted to strings so they can
> be stored in a bsddb (ie...str(record1)). My problem
> is, how do I convert the strings back to a
> dictionaries after removing the record from the bsddb?
> Any suggestions?

pickle.

Or [insert usual caveats about eval here]:

  $ python
  Python 2.2.1 (#1, Jun 25 2002, 10:55:46)
  [GCC 2.95.3-5 (cygwin special)] on cygwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> d = {'foo': 1, 'bar': 2}
  >>> s = repr(d)
  >>> d2 = eval(s)
  >>> d2 == d
  1

As they say in Wisconsin, and so.

// m
-





More information about the Python-list mailing list