On Mon, Aug 9, 2010 at 3:03 PM, aimeixu <[email protected]> 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
--
http://mail.python.org/mailman/listinfo/python-list