Dictionary to string and back to dictionary?? 
Author Message
 Dictionary to string and back to dictionary??

Hello all,
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?
Thanks,
Derek Basch

__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://www.*-*-*.com/



Wed, 09 Feb 2005 02:31:18 GMT  
 Dictionary to string and back to dictionary??

Quote:
> 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
-



Wed, 09 Feb 2005 03:18:38 GMT  
 Dictionary to string and back to dictionary??

Use the picke module:

http://www.python.org/doc/current/lib/module-pickle.html

Quote:

> Hello all,
> 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?
> Thanks,
> Derek Basch

> __________________________________________________
> Do You Yahoo!?
> Yahoo! Finance - Get real-time stock quotes
> http://finance.yahoo.com

> --
> http://mail.python.org/mailman/listinfo/python-list



Wed, 09 Feb 2005 03:15:59 GMT  
 Dictionary to string and back to dictionary??
Thanks everyone,
I think I will be using pickle because my trusty
'Python 2.1 Bible' doesn't cover eval that well and it
sounds like using pickle would be a little more
reliable.
Cya,
Derek Basch


Quote:
> > 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
> -

__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com


Wed, 09 Feb 2005 03:28:20 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Dictionary of dictionaries

2. losrt -dictionary and dictionary sorting...

3. Accessing Dictionary values from within the Dictionary

4. Placing String Controls with Dictionary Fields

5. Integer and String Dictionary Keys for Fast Access

6. Get dictionary-keys used to format a string

7. string/dictionary substitution error

8. Strings --> Dictionaries

9. convert dictionary to string

10. string to dictionary

11. string formatting with missing dictionary key

12. Convert String to Dictionary question

 

 
Powered by phpBB® Forum Software