<br><br><div class="gmail_quote">On Mon, Aug 9, 2010 at 3:03 PM, aimeixu <span dir="ltr"><<a href="mailto:aimeixu@amazon.com">aimeixu@amazon.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Hi,<br>
I am newbie for python ,Here is my question:<br>
a = "{'a':'1','b':'2'}"<br>
how to change a into a dictionary ,says, a = {'a':'1','b':'2'}<br>
Thanks a lot .Really need help.<br></blockquote><div><br></div><div>Parse the string and re-create the dictionary.</div><div><br></div><div>>>> s = "{'a':'1','b':'2'}"</div>

<div>>>> ds = {}</div><div>>>> for i in s.strip('{}').split(','):</div><div>...     key, val = i.split(':')</div><div>...     ds[key.strip("'")] = val.strip("'")</div>

<div>... </div><div>>>> ds</div><div>{'a': '1', 'b': '2'}</div><div>>>> type(ds)</div><div><class 'dict'></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<font color="#888888">
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>~l0nwlf<br>