简体   繁体   English

Python将字符串读入JSON

[英]Python reading string into json

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对象:

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 ? 而不是删除第一个"和最后一个"然后删除'"是否有更快的方法将此字符串转换为json对象?

You can use ast.literal_eval() : 您可以使用ast.literal_eval()

from ast import literal_eval

with open("input.txt") as f:
    for line in f:
        d = literal_eval(line)
        print(d)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM