Convert String to Dictionary question

MDK mdk at mdk.com
Thu Feb 14 16:19:58 EST 2002


"Gustavo Cordova" <gcordova at hebmex.com> wrote in message
news:mailman.1013720546.5030.python-list at python.org...
> >
> > I saved my dictionary {'hello1': [1, 0, 0]} to a file.
> >
> > When I do a readline the I get back a string.
> >
> > How do I convert this string to a dictionary?
> >
> > Thanks.
> >
>
> You eval() it?
>
> >>> d = { "one":1, "two":2, "three":3 }
> >>> print d
> {'three': 3, 'two': 2, 'one': 1}
> >>> S = str(d)
> >>> print S
> {'three': 3, 'two': 2, 'one': 1}
> >>> dd = eval(S)
> >>> print dd
> {'one': 1, 'three': 3, 'two': 2}
> >>> d is dd
> 0
> >>>
>
>
> Do I pass??
>
> :-)
>
> -gustavo
>

I never would have figured that one out.  Even after reading the
documentation it still does not appear that the eval() function would do
that.

Thanks for the info.





More information about the Python-list mailing list