如何从字典中写入HTML文件

时间:2013-10-25 16:22:01

标签: python html dictionary

features = get_featuresreply(features_reply)
with open('index1.html', 'w') as report:
    report.write('<html>')
    report.write('<body>')

    for key,value in flow_stats.itervalues():
        report.write(key)
        report.write(value)

我的功能字典看起来像这样

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

我无法将此dict写入HTML文件。有关如何解决此问题的任何建议吗?

  report.write(value)
TypeError: expected a character buffer object

我认为问题是因为字典是嵌套的,我无法将其写入HTML文件

1 个答案:

答案 0 :(得分:1)

您正试图迭代只是值,而不是键值对。请改用iteritems()

for key,value in flow_stats.iteritems():
    print key
    print value

接下来,当将整数(或其他任何不是字符串)写入文件时,首先将它们转换为字符串:

for key,value in flow_stats.iteritems():
    report.write(key)
    report.write(str(value))

但是使用string formatting会让您的任务变得更加容易:

for key,value in flow_stats.iteritems():
    report.write('<div>{}</div><div>{}</div>\n'.format(key, value))

因为字符串格式化允许您添加静态字符串并定义 值如何转换为字符串。