我正在尝试将数据导出到excel。
list = [{'ip': u'9.11.180.3','endpoint': u'rd2ff5-az2-1519-1520_eth1-18_vpc', 'hostname': u'RD2APIC1', 'encap': u'vlan-42', 'epg': u'19_14_140_172', 'extpaths': u'topology/pod-2/protpaths-1519-1520/pathep-[rd2ff5-az2-1519-1520_eth1-18_vpc]'},{'ip': u'14.10.5.5','endpoint': u'eth1/2','hostname': u'RD2APIC1', 'encap': u'vlan-18', 'epg': u'14_70_7_0', 'extpaths': u'topology/pod-2/paths-1511/extpaths-102/pathep-[eth1/2]'}]
我的代码:
filename = self.controller + ".xls"
excel_file = xlwt.Workbook()
sheet = excel_file.add_sheet('HOSTLIST')
sheet.write(0, 0, "APIC")
sheet.write(0, 1, "VLAN")
sheet.write(0, 2, "EPG")
sheet.write(0, 3, "EndPoint")
sheet.write(0, 4, "Extpath")
for count in xrange(1, len(list) + 1):
if list[count - 1]["epg"]:
epg = list[count - 1]["epg"]
else:
epg = "UNKNOWN"
sheet.write(count, 0, list[count - 1].get("hostname","UNKNOWN"))
sheet.write(count, 1, list[count - 1].get("encap","UNKNOWN"))
sheet.write(count, 2, epg)
sheet.write(count, 3, list[count - 1].get("endpoint","UNKNOWN"))
sheet.write(count, 4, list[count - 1].get("extpaths","UNKNOWN"))
excel_file.save(filename)
我已上传图像,有关当前和预期的输出。基本上,如果将“端点”以“ rd”开头,则需要将其导出到excel。
在这种情况下,“端点-rd2ff5-az2-1519-1520_eth1-18_vpc
”需要分为rd2ff5-az2-1519
和rd2ff5-az2-1520
并插入两行,而不是在Excel工作表中插入一行
有什么建议/想法吗?