我需要以正确的格式将嵌套字典写入 HTML 文件。就像 HTML Table 中的每个键值对一样。
with open('index1.html', 'w') as report:
report.write('<html>')
report.write('<body>')
report.write('<table border="1">')
report.write('<center>')
#for i in range(10):
for key,value in features.iteritems():
report.write('<tr>')
report.write('<td>{}</td><td>{}</td>\n'.format(key,value)
report.write('<tr>')
report.write('</center>')
report.write('</table>')
report.write('</html>')
report.write('</body>')
这就是我的字典的样子。
{
"1": {
"Port hw_addr": "[62, 74, 73, 133, 83, 75]",
"Port A advertised": 0,
"Port A config": 0,
"Port A curr": 192,
"Port A name": "veth0",
"Port A peer": 0,
"Port A state": 0,
"Port A supported": 0,
"port_no": 1
},
"2": {
"Port hw_addr": "[18, 24, 239, 62, 79, 131]",
"Port A advertised": 0,
"Port A config": 0,
"Port A curr": 192,
"Port A name": "veth2",
"Port A peer": 0,
"Port A state": 0,
"Port A supported": 0,
"port_no": 2
},
"3": {
"Port hw_addr": "[134, 91, 48, 58, 109, 123]",
"Port A advertised": 0,
"Port A config": 0,
"Port A curr": 192,
"Port A name": "veth4",
"Port A peer": 0,
"Port A state": 0,
"Port A supported": 0,
"port_no": 3
},
"4": {
"Port hw_addr": "[182, 146, 36, 212, 132, 231]",
"Port A advertised": 0,
"Port A config": 0,
"Port A curr": 192,
"Port A name": "veth6",
"Port A peer": 0,
"Port A state": 0,
"Port A supported": 0,
"port_no": 4
},
"65534": {
"Port hw_addr": "[0, 35, 32, 82, 18, 23]",
"Port A advertised": 0,
"Port A config": 0,
"Port A curr": 130,
"Port A name": "tap0",
"Port A peer": 0,
"Port A state": 0,
"Port A supported": 0,
"port_no": 65534
},
"No of Ports": 5,
"No of Tables": "2",
"OFPAT_ENQUEUE": true,
"OFPAT_OUTPUT": true,
"OFPAT_SET_DL_DST": true,
"OFPAT_SET_DL_SRC": true,
"OFPAT_SET_NW_DST": true,
"OFPAT_SET_NW_TOS": false,
"OFPAT_SET_TP_DST": true,
"OFPAT_SET_TP_SRC": true,
"OFPAT_SET_VLAN_PCP": true,
"OFPAT_SET_VLAN_VID": true,
"OFPAT_STRIP_VLAN": true,
"OFPC_ARP_MATCH_IP": true,
"OFPC_FLOW_STATS": true,
"OFPC_IP_REASM": false,
"OFPC_PORT_STATS": true,
"OFPC_QUEUE_STATS": true,
"OFPC_RESERVED": false,
"OFPC_STP": false,
"OFPC_TABLE_STATS": true,
"buffer_size": "256",
"datapath_id": true
}
键值 1、2、3、4 和 65545 是嵌套的。我想要一行中的键和下一列同一行中的相应值列表(格式正确)。它像字符串一样连续打印。