how to change a string into dictionary
Shashwat Anand
anand.shashwat at gmail.com
Mon Aug 9 05:53:38 EDT 2010
On Mon, Aug 9, 2010 at 3:03 PM, aimeixu <aimeixu at amazon.com> wrote:
> Hi,
> I am newbie for python ,Here is my question:
> a = "{'a':'1','b':'2'}"
> how to change a into a dictionary ,says, a = {'a':'1','b':'2'}
> Thanks a lot .Really need help.
>
Parse the string and re-create the dictionary.
>>> s = "{'a':'1','b':'2'}"
>>> ds = {}
>>> for i in s.strip('{}').split(','):
... key, val = i.split(':')
... ds[key.strip("'")] = val.strip("'")
...
>>> ds
{'a': '1', 'b': '2'}
>>> type(ds)
<class 'dict'>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100809/56d562e6/attachment-0001.html>
More information about the Python-list
mailing list