I have a list of dictionaries that have other dictionaries on them.
Dictionary:
[[{'id': 1, 'networkId': 'L_1111', 'name': 'VLAN1', 'applianceIp': '1.1.1.1', 'subnet': '1.1.1.0/24', 'fixedIpAssignments': {}, 'reservedIpRanges': [], 'dnsNameservers': 'upstream_dns', 'dhcpHandling': 'Run a DHCP server', 'dhcpLeaseTime': '1 day', 'dhcpBootOptionsEnabled': False, 'dhcpOptions': [], 'interfaceId': '1', 'networkName': 'NETWORK1'}, {'id': 2, 'networkId': 'L_2222', 'name': 'VLAN2', 'applianceIp': '2.2.2.2', 'subnet': '2.2.2.0/24', 'fixedIpAssignments': {}, 'reservedIpRanges': [], 'dnsNameservers': 'upstream_dns', 'dhcpHandling': 'Do not respond to DHCP requests', 'interfaceId': '2', 'networkName': 'NETWORK2'}]]
JSON version:
[
[
{
"id": 1,
"networkId": "L_1111",
"name": "VLAN1",
"applianceIp": "1.1.1.1",
"subnet": "1.1.1.0/24",
"fixedIpAssignments": {},
"reservedIpRanges": [],
"dnsNameservers": "upstream_dns",
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"dhcpBootOptionsEnabled": false,
"dhcpOptions": [],
"interfaceId": "1",
"networkName": "NETWORK1"
},
{
"id": 2,
"networkId": "L_2222",
"name": "VLAN2",
"applianceIp": "2.2.2.2",
"subnet": "2.2.2.0/24",
"fixedIpAssignments": {},
"reservedIpRanges": [],
"dnsNameservers": "upstream_dns",
"dhcpHandling": "Do not respond to DHCP requests",
"interfaceId": "2",
"networkName": "NETWORK2"
},
]
]
I am trying to move this to a CSV file. However, I haven't figured out how to do it. I tried with pandas library but it isn't giving me the output that I am looking for.
Something like this:
id,networkId,name,applianceIp,subnet,fixedIpAssignments,reservedIpRanges,dnsNameservers,dhcpHandling,interfaceId,networkName
1,L_1111,VLAN1,1.1.1.1,1.1.1.0/24,{},[],upstream_dns,Do not respond to DHCP requests,1,NETWORK1
2,L_2222,VLAN2,2.2.2.2,2.2.2.0/24,{},[],upstream_dns,Do not respond to DHCP requests,2,NETWORK2
Expected Output:
id networkId name applianceIP subnet
1 L_1111 VLAN1 1.1.1.1 1.1.1.0/24
2 L_2222 VLAN2 2.2.2.2 2.2.2.0/24
I'd look at using pandas to convert the list to a dataframe and then you'll be able to export that to a csv file.
import pandas as pd
data = [[{'id': 1, 'networkId': 'L_1111', 'name': '1', 'applianceIp': '1.1.1.1', 'subnet': '1.1.1.0/24', 'fixedIpAssignments': {}, 'reservedIpRanges': [], 'dnsNameservers': 'upstream_dns', 'dhcpHandling': 'Run a DHCP server', 'dhcpLeaseTime': '1 day', 'dhcpBootOptionsEnabled': False, 'dhcpOptions': [], 'interfaceId': '1', 'networkName': '1'}, {'id': 2, 'networkId': 'L_2222', 'name': '2', 'applianceIp': '2.2.2.2', 'subnet': '2.2.2.0/24', 'fixedIpAssignments': {}, 'reservedIpRanges': [], 'dnsNameservers': 'upstream_dns', 'dhcpHandling': 'Do not respond to DHCP requests', 'interfaceId': '2', 'networkName': '2'}]]
df = pd.DataFrame(data[0])
df.to_csv("output.csv")