python读取xml放入dict字典中
来源:互联网 发布:log4j.xml sql 编辑:程序博客网 时间:2024/06/05 19:08
<?
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。
1 0
- python读取xml放入dict字典中
- Python中dict 字典
- Python中dict字典使用方法
- Python中Dict字典用法
- Python中字典(dict)详解
- Python 中两个字典(dict)合并
- Python 中两个字典(dict)合并
- python中两个字典(dict)合并
- python中字典(dict)常见用法
- Python 中两个字典(dict)合并
- Python 中两个字典(dict)合并
- Python 中dict 字典方法小结
- Python 中dict字典get方法讲解
- Python 中两个字典(dict)合并
- 读取xml值放入droplist中
- python dict字典
- python dict字典
- python dict字典
- wxpythom入门
- CentOs6.5下配置nginx
- 关于UIEdgeInsetsMake
- UVa 694 - The Collatz Sequence
- 起先在女儿的房间里玩得不亦乐乎
- python读取xml放入dict字典中
- NAND flash和NOR flash的区别详解
- 去除浏览器首页的方法(hao123,2345...)
- ORA-01589错误的解决方法
- 我开博的目的——写在技术贴之前。
- 欢迎您在新浪博客安家
- Android中利用Fragment显示为两屏
- JSP+JavaBean+Servlet工作原理实例…
- Maven 3 入门 -- 安装与配置