I have a format of a string like this:
{'ind': 'a', 'system': 'x'}
and I would like to turn it into a dictionary, how do I do so?
I have a format of a string like this:
{'ind': 'a', 'system': 'x'}
and I would like to turn it into a dictionary, how do I do so?
Turning that string into JSON is easy enough, with .replace()
.
import json, pprint
s = "{'ind': 'a', 'system': 'x'}"
d = json.loads(s.replace("'", '"'))
assert isinstance(d, dict)
pprint.pp(d, width=1)
produces
{'ind': 'a',
'system': 'x'}
For more general cases, you may wish to rely on ast.parse or literal_eval.