how to change a string into dictionary

Discussion in 'Python' started by aimeixu, Aug 9, 2010.

  1. aimeixu

    aimeixu Guest

    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.
    aimeixu, Aug 9, 2010
    #1
    1. Advertising

  2. 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
    Ulrich Eckhardt, Aug 9, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    434
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    319
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,013
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    517
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    183
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page