I have been trying to convert a string of dictionary objects as given below
"{'Cmd': None, 'Hostname': None, 'Entrypoint': None, 'Env': None, 'OpenStdin': False,
'Tty': False, 'Domainname': None, 'Image': u'nginx:latest', 'HostConfig':
{'Binds': None, 'RestartPolicy': {}, 'NetworkMode': u'bridge', 'Dns': None,
'Links': [], 'PortBindings': {}, 'DnsSearch': None, 'Privileged': False,
'VolumesFrom': []}, 'ExposedPorts': {}, 'User': None}"
to python dictionary.
But the solutions came up while searching were suggesting to use python ast library for the purpose. as
import ast
ast.literal_eval(string)
And it works fine. But is ast library is built indented to solve problems like this ? Is there any good approach other than this to solve the problem ?
json
, but your string is not quite valid JSON (None
instead ofnull
, etc.) -- if it were, you could simply usejson.loads
from the Python standard library. \$\endgroup\$