使用ast.literal_eval时格式错误的字符串

时间:2017-12-08 08:31:07

标签: python json dictionary unicode

我使用json dump然后json加载相同的数据。数据是unicode所以我将其转换为字符串。使用ast.literla_eval我试图得到字符串的类型dict但我得到错误格式错误的字符串。

json load的输出低于

('data', u'{\n  "a": "spawning", \n  "addresses": "", \n  "image": "b", \n  "OS-EXT-STS:vm_state": "building", \n  "c:launched_at": null, \n  "d": "e (fgh)", \n  "user_id": "hhh", \n 
    "accessIPv4": "", \n  "accessIPv6": "", \n  "name": "kk", \n  "created": "2017-12-08T07:52:44Z", \n  "z:xyz": []\n}', <type 'unicode'>)

我尝试了什么?

        with open('openstack_list.json', 'w') as e:
            json.dump(check_output(['openstack', 'server', 'show', i, '-f', 'json']), e)
        with open('openstack_list.json', 'r') as a:
            data = json.load(a)
            new_data = data.encode('utf-8')  # output type is unicode
            dict_data = ast.literal_eval(new_data) # output type is string

我希望输出是字典,但我没有得到它。此外,json load提供了unicode数据,因此我认为new_data = data.encode('utf-8')是多余的。但是,如果我在没有编码的情况下使用ast.literal_eval,我会收到错误的字符串错误。无论如何,我无法将数据类型变为字典。

编辑:

错误:

Traceback (most recent call last):
  File "openstack_resource_list.py", line 84, in <module>
    output = get_resources()
  File "openstack_resource_list.py", line 47, in get_resources
    dict_data = ast.literal_eval(new_data)
  File "/usr/lib64/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib64/python2.7/ast.py", line 63, in _convert
    in zip(node.keys, node.values))
  File "/usr/lib64/python2.7/ast.py", line 62, in <genexpr>
    return dict((_convert(k), _convert(v)) for k, v
  File "/usr/lib64/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

json dump之前的数据:

"{\n  \"aaa\": null, \n  \"addresses\": \"inner-net=192.168.0.173, x.x.x.x\", \n  \"image\": \"aaa (aaa)\", 
    \n  \"aaa:vm_state\": \"active\", \n  \"aaa:launched_at\": \"2017-12-08T08:21:45.000000\", \n  \"flavor\": \"aaa4 (aaa)\", 
    \n  \"id\": \"aaa\", \n  \"security_groups\": [\n    {\n      \"name\": \"default\"\n    }\n  ], \n  \"user_id\": \"aaa\", 
    \n  \"OS-DCF:diskConfig\": \"MANUAL\", \n  \"accessIPv4\": \"\", \n  \"accessIPv6\": \"\", \n  \"progress\": 0, \n  \"Oaa:power_state\": 1, \n  \"project_id\": \"aaa\", 
    \n  \"config_drive\": \"\", \n  \"status\": \"ACTIVE\", \n  \"updated\": \"2017-12-08T08:21:45Z\", \n  \"hostId\": \"aaa\", \n  \"OS-SRV-USG:terminated_at\": null, 
    \n  \"key_name\": \"pg_ci\", \n  \"properties\": \"\", \n  \"OS-EXT-AZ:availability_zone\": \"nova\", \n  \"name\": \"taaa\", \n  \"created\": \"2017-12-08T08:21:31Z\", \n 
    \"os-extended-volumes:volumes_attached\": [\n    {\n      \"id\": \"aaa\"\n    }\n  ]\n}"

1 个答案:

答案 0 :(得分:3)

check_output返回的数据已经是JSON,所以你应该 JSON-ify再次使用json.dump。您可以按原样将其写入文件,该文件将是有效的JSON文件。您可以使用json.loads

将其加载到Python对象中
import json
from pprint import pprint

s = """{\n  \"aaa\": null, \n  \"addresses\": \"inner-net=192.168.0.173, x.x.x.x\", \n  \"image\": \"aaa (aaa)\", 
    \n  \"aaa:vm_state\": \"active\", \n  \"aaa:launched_at\": \"2017-12-08T08:21:45.000000\", \n  \"flavor\": \"aaa4 (aaa)\", 
    \n  \"id\": \"aaa\", \n  \"security_groups\": [\n    {\n      \"name\": \"default\"\n    }\n  ], \n  \"user_id\": \"aaa\", 
    \n  \"OS-DCF:diskConfig\": \"MANUAL\", \n  \"accessIPv4\": \"\", \n  \"accessIPv6\": \"\", \n  \"progress\": 0, \n  \"Oaa:power_state\": 1, \n  \"project_id\": \"aaa\", 
    \n  \"config_drive\": \"\", \n  \"status\": \"ACTIVE\", \n  \"updated\": \"2017-12-08T08:21:45Z\", \n  \"hostId\": \"aaa\", \n  \"OS-SRV-USG:terminated_at\": null, 
    \n  \"key_name\": \"pg_ci\", \n  \"properties\": \"\", \n  \"OS-EXT-AZ:availability_zone\": \"nova\", \n  \"name\": \"taaa\", \n  \"created\": \"2017-12-08T08:21:31Z\", \n 
    \"os-extended-volumes:volumes_attached\": [\n    {\n      \"id\": \"aaa\"\n    }\n  ]\n}"""

d = json.loads(s)
pprint(d)

<强>输出

{'OS-DCF:diskConfig': 'MANUAL',
 'OS-EXT-AZ:availability_zone': 'nova',
 'OS-SRV-USG:terminated_at': None,
 'Oaa:power_state': 1,
 'aaa': None,
 'aaa:launched_at': '2017-12-08T08:21:45.000000',
 'aaa:vm_state': 'active',
 'accessIPv4': '',
 'accessIPv6': '',
 'addresses': 'inner-net=192.168.0.173, x.x.x.x',
 'config_drive': '',
 'created': '2017-12-08T08:21:31Z',
 'flavor': 'aaa4 (aaa)',
 'hostId': 'aaa',
 'id': 'aaa',
 'image': 'aaa (aaa)',
 'key_name': 'pg_ci',
 'name': 'taaa',
 'os-extended-volumes:volumes_attached': [{'id': 'aaa'}],
 'progress': 0,
 'project_id': 'aaa',
 'properties': '',
 'security_groups': [{'name': 'default'}],
 'status': 'ACTIVE',
 'updated': '2017-12-08T08:21:45Z',
 'user_id': 'aaa'}

如果你想把它变成干净的JSON,把那个Python对象传递给json.dumpjson.dumps

print(json.dumps(d, indent=4))

<强>输出

{
    "aaa": null,
    "addresses": "inner-net=192.168.0.173, x.x.x.x",
    "image": "aaa (aaa)",
    "aaa:vm_state": "active",
    "aaa:launched_at": "2017-12-08T08:21:45.000000",
    "flavor": "aaa4 (aaa)",
    "id": "aaa",
    "security_groups": [
        {
            "name": "default"
        }
    ],
    "user_id": "aaa",
    "OS-DCF:diskConfig": "MANUAL",
    "accessIPv4": "",
    "accessIPv6": "",
    "progress": 0,
    "Oaa:power_state": 1,
    "project_id": "aaa",
    "config_drive": "",
    "status": "ACTIVE",
    "updated": "2017-12-08T08:21:45Z",
    "hostId": "aaa",
    "OS-SRV-USG:terminated_at": null,
    "key_name": "pg_ci",
    "properties": "",
    "OS-EXT-AZ:availability_zone": "nova",
    "name": "taaa",
    "created": "2017-12-08T08:21:31Z",
    "os-extended-volumes:volumes_attached": [
        {
            "id": "aaa"
        }
    ]
}

在原始JSON中,键按字母顺序排序。要在已清理的JSON中执行此操作,只需将sort_keys=True传递给json.dumps