Python列表到JSON的问题

时间:2018-06-13 11:08:41

标签: python python-3.x dictionary

所以我有一个列表列表,我试图转换为字典,然后转换为JSON对象。

列表清单示例

[
    ['service:3', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:6184', 'startname:Account\Name'],
    ['service:7', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:11188', 'startname:Account\Name'],
    ['service:4', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:8268', 'startname:Account\Name'],
    ['service:6', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:8528', 'startname:Account\Name'],
    ['service:2', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:2308', 'startname:Account\Name'],
    ['service:9', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:31380', 'startname:Account\Name'],
    ['service:1', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:2208', 'startname:Account\Name'],
    ['service:5', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:21468', 'startname:Account\Name'],
    ['service:8', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:21984', 'startname:Account\Name'],
    ['service:10', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:7560', 'startname:Account\Name']
]

我想将其转换为嵌套字典,其中上层是服务器IP地址。

潜在词典示例

{Server:Ipaddress 
    {[
    {service: 3, "name": "Name1", "description": "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 6184, "startname": "Account\Name"},
    {service: 7, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 11188, "startname": "Account\Name"},
    {service: 4, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 8268, "startname": "Account\Name"},
    {service: 6, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 8528, "startname": "Account\Name"},
    {service: 2, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 2308, "startname": "Account\Name"},
    {service: 9, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 31380, "startname": "Account\Name"},
    {service: 1, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 2208, "startname": "Account\Name"},
    {service: 5, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 21468, "startname": "Account\Name"},
    {service: 8, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 21984, "startname": "Account\Name"},
    {service: 10, "name": "Name1", description: "What it does", display"name": "display name of service", "pathname": "Dir path to executable", "processid": 7560, "startname": "Account\Name"}
    ]}
}

我试过dict1 = dict(list1)但是,我得到了builtins.ValueError: dictionary update sequence element #0 has length 7; 2 is required

我也试图

for item in list1:
    for i in item:
       dict1 = dict(i)

这当然也没有用。 builtins.ValueError: dictionary update sequence element #0 has length 1; 2 is required

我没有尝试过,我不确定如何在更高级别包含服务器详细信息。从我读过的内容我应该将列表列表移动到一个字典然后创建一个带有服务器值的新字典并添加前一个字典中的数据?

1 个答案:

答案 0 :(得分:3)

主要问题似乎是您尝试从列表列表中创建字典列表。

请注意,在每个子列表中,您需要按":"拆分每个项目。 str.split允许您将每个此类实例拆分为一个列表。 dict可以接受这些列表的迭代来创建字典。然后,您可以在外部列表中的每个子列表上执行此操作。

下面是一个使用列表推导的工作示例。

L = [['service:3', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:6184', r'startname:Account\Name'],
     ['service:7', 'name:Name1', 'description:What it does', 'displayname:display name of service', 'pathname:Dir path to executable', 'processid:11188', r'startname:Account\Name']]

res = [dict(i.split(':') for i in item) for item in L]

print(res)

[{'description': 'What it does',
  'displayname': 'display name of service',
  'name': 'Name1',
  'pathname': 'Dir path to executable',
  'processid': '6184',
  'service': '3',
  'startname': 'Account\\Name'},
 {'description': 'What it does',
  'displayname': 'display name of service',
  'name': 'Name1',
  'pathname': 'Dir path to executable',
  'processid': '11188',
  'service': '7',
  'startname': 'Account\\Name'}]