1

I have this string

"{u'Status': u'Up About an hour', u'Created': 1468874455, u'Image': u'instavote/vote', u'Labels': {u'com.docker.compose.service': u'webapp', u'com.docker.compose.config-hash': u'01e63b02746f28876c67969a7dfb39cd68ee598b8edc062a00a2812114c660a1', u'com.docker.compose.project': u'vote', u'com.docker.compose.version': u'1.6.2', u'com.docker.compose.oneoff': u'False', u'com.docker.compose.container-number': u'2'}, u'NetworkSettings': {u'Networks': {u'vote_default': {u'NetworkID': u'', u'MacAddress': u'02:42:ac:13:00:02', u'GlobalIPv6PrefixLen': 0, u'Links': None, u'GlobalIPv6Address': u'', u'IPv6Gateway': u'', u'IPAMConfig': None, u'EndpointID': u'6aeb163ac91970e9d5d62edc99f8ee27e5ac696fb0f662859ee9da097dcf4df5', u'IPPrefixLen': 16, u'IPAddress': u'172.19.0.2', u'Gateway': u'172.19.0.1', u'Aliases': None}}}, u'HostConfig': {u'NetworkMode': u'vote_default'}, u'ImageID': u'sha256:cfcbf877123035ee3c458842c72d1d2204043fad686e9433353c69ed5ed762e5', u'State': u'running', u'Command': u'gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0', u'Names': [u'/vote_webapp_2'], u'Mounts': [], u'Id': u'5341fc7297ce047bda8d55db7b61b0265c97d050ac6eb163a084b28301de1a2f', u'Ports': [{u'Type': u'tcp', u'PrivatePort': 80}]}"
"{u'Status': u'Up About an hour', u'Created': 1468874455, u'Image': u'instavote/vote', u'Labels': {u'com.docker.compose.service': u'webapp', u'com.docker.compose.config-hash': u'01e63b02746f28876c67969a7dfb39cd68ee598b8edc062a00a2812114c660a1', u'com.docker.compose.project': u'vote', u'com.docker.compose.version': u'1.6.2', u'com.docker.compose.oneoff': u'False', u'com.docker.compose.container-number': u'2'}, u'NetworkSettings': {u'Networks': {u'vote_default': {u'NetworkID': u'', u'MacAddress': u'02:42:ac:13:00:02', u'GlobalIPv6PrefixLen': 0, u'Links': None, u'GlobalIPv6Address': u'', u'IPv6Gateway': u'', u'IPAMConfig': None, u'EndpointID': u'6aeb163ac91970e9d5d62edc99f8ee27e5ac696fb0f662859ee9da097dcf4df5', u'IPPrefixLen': 16, u'IPAddress': u'172.19.0.2', u'Gateway': u'172.19.0.1', u'Aliases': None}}}, u'HostConfig': {u'NetworkMode': u'vote_default'}, u'ImageID': u'sha256:cfcbf877123035ee3c458842c72d1d2204043fad686e9433353c69ed5ed762e5', u'State': u'running', u'Command': u'gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0', u'Names': [u'/vote_webapp_2'], u'Mounts': [], u'Id': u'5341fc7297ce047bda8d55db7b61b0265c97d050ac6eb163a084b28301de1a2f', u'Ports': [{u'Type': u'tcp', u'PrivatePort': 80}]}"

that I would like to read into a json object using :

json.loads(json.dumps(c))

rather than removing the first " and the last " then changing ' by " is there a faster way to convert this string into a json object ?

asked Jul 19, 2016 at 18:57
1
  • use sed command to perform replace operation and then load into python, if the data has been loaded from a txt file. Commented Jul 19, 2016 at 19:07

1 Answer 1

1

You can use ast.literal_eval():

from ast import literal_eval
with open("input.txt") as f:
 for line in f:
 d = literal_eval(line)
 print(d)
answered Jul 19, 2016 at 19:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.