Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to change a string into dictionary

Reply
Thread Tools

how to change a string into dictionary

 
 
aimeixu
Guest
Posts: n/a
 
      08-09-2010
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.
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      08-09-2010
aimeixu wrote:
> 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

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM



Advertisments