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。
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值