如何访问字典键和值列表中的元素以在 Python 中写入 csv 行

问题描述 投票:0回答:1

我的代码用于将单个 json 字典键和值写入 csv 文件,在平面字典上迭代,就像这样。

"data": {"id": 1702, "subnet": "10.111.0.0", "mask": "21", "sectionId": 3, "description": "POD", "linked_subnet": null, "firewallAddressObject": null, "vrfId": null, "masterSubnetId": 1158, "allowRequests": 0, "vlanId": null, "showName": 0, "device": null, "permissions": "{\"4\":\"3\"}", "pingSubnet": 0, "discoverSubnet": 0, "resolveDNS": 0, "DNSrecursive": 0, "DNSrecords": 0, "nameserverId": 0, "scanAgent": 0, "customer_id": null, "isFolder": 0, "isFull": 0, "isPool": 0, "tag": 2, "threshold": 0, "location": null, "editDate": null, "lastScan": null, "lastDiscovery": null, "calculation": {"Type": "IPv4", "IP address": "/", "Network": "10.111.0.0", "Broadcast": "10.111.7.255", "Subnet bitmask": "21", "Subnet netmask": "255.255.248.0", "Subnet wildcard": "0.0.7.255", "Min host IP": "10.111.0.1", "Max host IP": "10.111.7.254", "Number of hosts": "2046", "Subnet Class": "private A"}}

我的代码是:

rawdata = json.loads(res.content)
json_data_dict = json.dumps(rawdata)
print(f"\nPRINTING JSON DICT DATA\n {json_data_dict}")

json_parse = json.loads(json_data_dict, object_pairs_hook=OrderedDict)

with open('ipamsubnet.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(json_parse["data"].keys())  # header row
    writer.writerow(json_parse["data"].values())

csv 行看起来很好,如下所示:

我现在要做的下一件事是访问字典列表中的嵌套字典元素,因为前面提到的主子网下有一些子网需要在 csv 中考虑,但数据结构不再是字典,而是一个字典列表,句柄仍然是

data
,如下所示。至少,这就是我认为我正在努力做的事情。以下是数据结构的摘录:

"data": [{"id": 1713, "subnet": "10.111.0.0", "mask": "27", "sectionId": 3, "description": "POD_Site", "linked_subnet": null, "firewallAddressObject": null, "vrfId": null, "masterSubnetId": 1702, "allowRequests": 0, "vlanId": null, "showName": 0, "device": null, "permissions": "{\"4\":\"3\"}", "pingSubnet": 0, "discoverSubnet": 0, "resolveDNS": 0, "DNSrecursive": 0, "DNSrecords": 0, "nameserverId": 0, "scanAgent": 0, "customer_id": null, "isFolder": 0, "isFull": 0, "isPool": 0, "tag": 2, "threshold": 0, "location": null, "editDate": null, "lastScan": null, "lastDiscovery": null}, {"id": 1714, "subnet": "10.111.0.32", "mask": "27", "sectionId": 3, "description": "POD_Site", "linked_subnet": null, "firewallAddressObject": null, "vrfId": null, "masterSubnetId": 1702, "allowRequests": 0, "vlanId": null, "showName": 0, "device": null, "permissions": "{\"4\":\"3\"}", "pingSubnet": 0, "discoverSubnet": 0, "resolveDNS": 0, "DNSrecursive": 0, "DNSrecords": 0, "nameserverId": 0, "scanAgent": 0, "customer_id": null, "isFolder": 0, "isFull": 0, "isPool": 0, "tag": 2, "threshold": 0, "location": null, "editDate": null, "lastScan": null, "lastDiscovery": null}, {"id": 1715, "subnet": "10.111.0.64", "mask": "27", "sectionId": 3, "description": "POD_Site", "linked_subnet": null, "firewallAddressObject": null, "vrfId": null, "masterSubnetId": 1702, "allowRequests": 0, "vlanId": null, "showName": 0, "device": null, "permissions": "{\"4\":\"3\"}", "pingSubnet": 0, "discoverSubnet": 0, "resolveDNS": 0, "DNSrecursive": 0, "DNSrecords": 0, "nameserverId": 0, "scanAgent": 0, "customer_id": null, "isFolder": 0, "isFull": 0, "isPool": 0, "tag": 2, "threshold": 0, "location": null, "editDate": null, "lastScan": null, "lastDiscovery": null}, {"id": 1716, "subnet": "10.111.0.96", "mask": "27", "sectionId": 3, "description": "POD_Site", "linked_subnet": null, "firewallAddressObject": null, "vrfId": null, "masterSubnetId": 1702, "allowRequests": 0, "vlanId": null, "showName": 0, "device": null, "permissions": "{\"4\":\"3\"}", "pingSubnet": 0, "discoverSubnet": 0, "resolveDNS": 0, "DNSrecursive": 0, "DNSrecords": 0, "nameserverId": 0, "scanAgent": 0, "customer_id": null, "isFolder": 0, "isFull": 0, "isPool": 0, "tag": 2, "threshold": 0, "location": null, "editDate": null, "lastScan": null, "lastDiscovery": null}, {"id": 1718, "subnet": "10.111.0.128", "mask": "27", "sectionId": 3, "description": "POD_Site", "linked_subnet": null, "firewallAddressObject": null, "vrfId": null, "masterSubnetId": 1702, "allowRequests": 0, "vlanId": null, "showName": 0, "device": null, "permissions": "{\"4\":\"3\"}", "pingSubnet": 0, "discoverSubnet": 0, "resolveDNS": 0, "DNSrecursive": 0, "DNSrecords": 0, "nameserverId": 0, "scanAgent": 0, "customer_id": null, "isFolder": 0, "isFull": 0, "isPool": 0, "tag": 2, "threshold": 0, "location": null, "editDate": null, "lastScan": null, "lastDiscovery": null},...

data
现在是一个字典列表的字典,其中
data
是键,值是一个字典列表,还是只是一个字典列表?

目标是让csv包含根据每个

id
键的行数,这表示另一个字典的开始,每个字典都是一个列表索引元素,至少这是我的理解和想象.

这样的事情是否朝着正确的方向发展:

'Key': list(data)[0]
'Value': list(data.values()[0]

非常感谢任何指导。

python json list dictionary export-to-csv
1个回答
0
投票

你可以使用pandas

import pandas as pd

json_parse = {"data" : [
    {'id': 1713, 'subnet': '10.111.0.0', 'mask': '27'},
    {'id': 1714, 'subnet': '10.111.0.32', 'mask': '27'},
    {'id': 1715, 'subnet': '10.111.0.64', 'mask': '27'}
]}

# create DataFrame
df = pd.DataFrame(json_parse["data"])

# save csv file
df.to_csv('ipamsubnet.csv', index=False)
© www.soinside.com 2019 - 2024. All rights reserved.