how to change a string into dictionary

A

aimeixu

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.
 
U

Ulrich Eckhardt

aimeixu said:
a = "{'a':'1','b':'2'}"
how to change a into a dictionary ,says, a = {'a':'1','b':'2'}

You could evaluate it as regular Python code, using "exec":

res = {}
exec("a={'a':'1'}", res)
print res['a']

However, if this is input from a file or the user, be aware that this opens
loopholes for executing _any_ code, so you should only exec code from
sources you can trust.

Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top