[python-3.x] 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 the eval() function?

What I really want is a function which can directly convert a dictionary to a string.

This question is related to python-3.x

The answer is


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

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

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