How to convert string into dictionary in python
How to convert string into dictionary in python.
Today we are looking at how to convert string into dictionary in python.
Solution 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}")
Solution 2
json.loads
but it would require your string to use double quotes:
import json
python_dict = json.loads('{"a": 1}')