python读取xml放入dict字典中

<?xml version="1.0" encoding="utf-8"?>
<config desc="Line8Center" line="8">
    <stations>
        <ip base="192.168.8" />
        <station name="市光路站" />
        <station name="嫩江路站" />
        <station name="翔殷路站" />
        <station name="黄兴公园站" />
        <station name="延吉中路站" />
        <station name="黄兴路站" />
        <station name="江浦路站" />
        <station name="鞍山新村站" />
        <station name="四平路站" />
        <station name="曲阳路站" />
        <station name="虹口足球场站" />
        <station name="*北路站" />
        <station name="中兴路站" />
        <station name="曲阜路站" />
        <station name="人民广场站" />
        <station name="大世界站" />
        <station name="老西门站" />
        <station name="陆家浜路站" />
        <station name="*南路站" />
        <station name="周家渡路站" />
        <station name="耀华路站" />
        <station name="成山路站" />
        <station name="杨思站" />
        <station name="济阳路站" />
        <station name="凌兆新村" />
        <station name="芦恒路站" />
        <station name="浦江镇站" />
        <station name="江月路站" />
        <station name="浦江世博家园" />
        <station name="航天公园" />
        <station name="车辆段" is_park="True" />
        <station name="停车场" is_park="True" />
    </stations>
    <boxes>
        <box name="防灾调" id="1" port="9" in="3" rec="8" />
        <box name="总调" id="2" port="7" in="1" rec="6" />
        <box name="列调" id="3" port="8" in="2" rec="7" />
    </boxes>
    <pis remote="192.168.8.223:1001" />
</config>
#####################################################
# Create your views here.
# -*- coding: utf-8 -*-
import elementtree.ElementTree as ET


def attr_gbk(elem, name):
    return elem.attrib.get(name).encode("GBK")


def attr_int(elem, name):
    return int(elem.attrib.get(name))


def attr_bool(elem, name):
    return bool(elem.attrib.get(name))


def attr(elem, *args):
    if len(args) > 1:
        result = []
        for name in args:
            result.append(attr(elem, name))
        return result


    [name] = args
    value = elem.attrib.get(name)
    if not value:
        return value
    elif isinstance(value, unicode):
        return value.encode("GBK")
    elif value.isdigit():
        return int(value)
    elif value.lower() in ["true", "false"]:
        return value.lower() == "true"
    else:
        return value


def fix_attribs(elem):
    dict_ = {}
    for key in elem.attrib:
        dict_.update({key: attr(elem, key)})
    return dict_


def build_dict(elem):
    if elem is not None:
        dict_ = {}


        for subelem in elem:
            print subelem.tag
            if subelem.tag in dict_:
                if not isinstance(dict_[subelem.tag], list):
                    dict_[subelem.tag] = [dict_[subelem.tag]]
                dict_[subelem.tag].append(build_dict(subelem))
            else:
                dict_.update({subelem.tag: build_dict(subelem)})
            if subelem.text and subelem.text.strip():
                dict_.update({subelem.tag: {"_text": subelem.text}})
        dict_.update(fix_attribs(elem))
        return dict_
    else:
        return fix_attribs(elem)


if ‘__main__‘==__name__:
    tree = ET.parse("user.xml")
    root = tree.getroot()


    print build_dict(root)
##########################################
注意点:
所以对于<node></node>之间的正文反而用_text来表示了,需要用dict_["node"]["_text"]的方式来访问。
对于<node attr1="123"></node>则可以直接通过dict_["node"]["attr1"]访问属性attr1。

python读取xml放入dict字典中,布布扣,bubuko.com

python读取xml放入dict字典中

上一篇:Java编码规范


下一篇:tensorflow报错问题及解决方案记录页