0

我需要以正确的格式将嵌套字典写入 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 是嵌套的。我想要一行中的键和下一列同一行中的相应值列表(格式正确)。它像字符串一样连续打印。

4

2 回答 2

1

创建一个简单的语言模板,在那些已经制作的语言之间使用另一种,比如Jinja2

def recurse(dict):
    if type(dict)==type({}):
    for k in dict:
        recurse(dict[k])
    else:
        print_to_html(dict)
于 2013-10-25T19:48:36.377 回答
0

我已经把真假对真假。

我也关闭了错误的 tr 并交换了关闭的正文和 html

features = {
"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
}



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('</body>')
    report.write('</html>')
于 2013-10-25T19:44:53.953 回答