[PYTHON] xml.etree.ElementTree 实例

本文介绍了一种使用Python将XML文件转换为字典列表的方法,并通过具体示例展示了如何遍历XML节点及其属性。

---------------------------------------------------------------

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

def _convert_xml_to_dict_list (file_name):
    keyword_name = "_convert_xml_to_dict_list "
    try :
        tree = ET.parse(file_name)
        for child in tree.iter() :
            print ("tag:%s, attrib:%s" % (child.tag,child.attrib))

    except Exception as inst :
        s=sys.exc_info()
        raise AssertionError("%s:%s-> fail to parse xml to dict list, line: %s, excpetion: %s" \
        % (__name__, keyword_name, s[2].tb_lineno, str(inst)))

-------------------------------------------------------------------------------

Pay attathen to tag and attrib

tag:pdml, attrib:{'capture_file': '/home/qihz/dump_eth0.pcap', 'version': '0', 'time': 'Wed Aug 10 23:10:31 2016', 'creator': 'wireshark/1.10.0'} 

tag:packet, attrib:{} tag:proto, attrib:{'size': '60', 'name': 'geninfo', 'showname': 'General information', 'pos': '0'} 

tag:field, attrib:{'name': 'num', 'show': '4', 'pos': '0', 'value': '4', 'showname': 'Number', 'size': '60'} 

tag:field, attrib:{'name': 'len', 'show': '60', 'pos': '0', 'value': '3c', 'showname': 'Frame Length', 'size': '60'} 

tag:field, attrib:{'name': 'caplen', 'show': '60', 'pos': '0', 'value': '3c', 'showname': 'Captured Length', 'size': '60'}

 

转载于:https://my.oschina.net/activehealth/blog/731055

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值