-1
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
}

我无法将此字典写入 HTML 文件。对于如何解决这个问题,有任何的建议吗?

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

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

4

1 回答 1

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))

但是使用字符串格式会让你的任务更容易:

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

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

于 2013-10-25T16:25:37.997 回答