Skip to content Skip to sidebar Skip to footer

How To Convert String Into Dictionary In Python 3.*?

I want to convert the following string into dictionary without using eval() function in Python 3.5. d='{'Age': 7, 'Name': 'Manni'}'; Can anybody tell me the good way than using th

Solution 1:

  1. literal_eval, a somewhat safer version of eval (will only evaluate literals ie strings, lists etc):

    from ast importliteral_evalpython_dict= literal_eval("{'a': 1}")
    
  2. json.loads but it would require your string to use double quotes:

    importjsonpython_dict= json.loads('{"a": 1}')
    

Post a Comment for "How To Convert String Into Dictionary In Python 3.*?"