0

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?

JakeLong
  • 9
  • 3

1 Answers1

0

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.

J_H
  • 17,926
  • 4
  • 24
  • 44