如何将xml转为python中的字典
如何将xml转为python中的字典
import cElementTree as ElementTree class XmlListConfig(list):
def __init__(self, aList):
for element in aList:
if element:
# treat like dict
if len(element) == 1 or element[0].tag != element[1].tag:
self.append(XmlDictConfig(element))
# treat like list
elif element[0].tag == element[1].tag:
self.append(XmlListConfig(element))
elif element.text:
text = element.text.strip()
if text:
self.append(text) class XmlDictConfig(dict):
'''
Example usage: >>> tree = ElementTree.parse('your_file.xml')
>>> root = tree.getroot()
>>> xmldict = XmlDictConfig(root) Or, if you want to use an XML string: >>> root = ElementTree.XML(xml_string)
>>> xmldict = XmlDictConfig(root) And then use xmldict for what it is... a dict.
'''
def __init__(self, parent_element):
if parent_element.items():
self.update(dict(parent_element.items()))
for element in parent_element:
if element:
# treat like dict - we assume that if the first two tags
# in a series are different, then they are all different.
if len(element) == 1 or element[0].tag != element[1].tag:
aDict = XmlDictConfig(element)
# treat like list - we assume that if the first two tags
# in a series are the same, then the rest are the same.
else:
# here, we put the list in dictionary; the key is the
# tag name the list elements all share in common, and
# the value is the list itself
aDict = {element[0].tag: XmlListConfig(element)}
# if the tag has attributes, add those to the dict
if element.items():
aDict.update(dict(element.items()))
self.update({element.tag: aDict})
# this assumes that if you've got an attribute in a tag,
# you won't be having any text. This may or may not be a
# good idea -- time will tell. It works for the way we are
# currently doing XML configuration files...
elif element.items():
self.update({element.tag: dict(element.items())})
# finally, if there are no child tags and no attributes, extract
# the text
else:
self.update({element.tag: element.text})
用法示例:
tree = ElementTree.parse('your_file.xml')
root = tree.getroot()
xmldict = XmlDictConfig(root)
//或者,如果你想使用一个XML字符串:
root = ElementTree.XML(xml_string)
xmldict = XmlDictConfig(root) 学习了
https://cloud.tencent.com/developer/ask/51220
http://code.activestate.com/recipes/410469-xml-as-dictionary/
如何将xml转为python中的字典的更多相关文章
- Python中的字典与集合
今天我们来讲一讲python中的字典与集合 Dictionary:字典 Set:集合 字典的语法: Dictionary字典(键值对) 语法: dictionary = {key:value,key: ...
- python中 字符 字典 列表之间的转换
1 字典 转 字符 定义一个字典:dict = {'name': 'python', 'age': 7}字典转字符 可以使用str强制转换 如: str(dict) 此时dict的类型就是字符型了 2 ...
- 13.python中的字典
字典其实和之前的元祖和列表功能相似,都是用来储存一系列对象的.也就是一种可变容器,或者是我所比喻的革新派的菜单. 但也不是完全相同,我在之前曾经将字典称为特殊的'序列',是字典拥有序列的部分特性,但是 ...
- python中的字典(dict),列表(list),元组(tuple)
一,List:列表 python内置的一种数据类型是列表:list.list是一种有序的数据集合,可以随意的添加和删除其中的数据.比如列出班里所有的同学的名字,列出所有工厂员工的工号等都是可以用到列表 ...
- Python中字符串/字典/json之间的转换
import json #定义一个字典d1,字典是无序的 d1 = { "a": None, "b": False, "c": True, ...
- python中的字典 和 集合
python中字典是一种key-value的数据类型 字典的特性: 1.无序的 2.key必须的唯一的,so,字典天生去重 语法: 增加 修改 删除 查找 多级字典嵌套及操作 字典的其他用法 #set ...
- python中的字典内置方法小结
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- #key-value #dict 无序,无下标,不需要下标,因为有key stu={ 'stu001': ...
- python学习之【第六篇】:Python中的字典及其所具有的方法
1.前言 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,因此,字典的key必须是可哈 ...
- Python中的字典和集合
一.字典(dict) 1. 概述 字典是Python唯一的映射类型. 只能使用不可变的对象(比如字符串)来作为字典的键,但是可以把不可变或可变的对象作为字典的值. 键值对在 ...
随机推荐
- centOS7.4服务器 yum安装 搭建lamp环境
// 红色加粗是linux命令 安装gcc和gcc-c++ yum -y install gcc gcc-c++ yum list httpd* 安装apche yum -y install http ...
- bzoj3456 城市规划 多项式求In
\(n\)个点的无向联通图的个数 打着好累啊 一定要封装一个板子 记\(C(x)\)为无向图个数的指数型生成函数,\(C(0) = 1\) 记\(G(x)\)为无向联通图个数的指数型生成函数,\(G( ...
- 【HDU】2866:Special Prime【数论】
Special Prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- linux下使用cronjob定时执行php脚本
在linux中输入命令 crontab -e 然后使用vim的命令编辑打开的文件,输入 * * * * /usr/bin/php -f /home/userxxx/update.php 保存,退出,好 ...
- Linux性能监控分析命令(二)—sar命令介绍
性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof ======= ...
- MYSQL-5.5.37-win32.msi 这个版本得程序包谁有吗 可以给我一下吗?
之前下载了这个版本得mysql 但是跟服务器链接不上 后来我就卸载了 但由于卸载不干净 现在又删了注册表 好像把这个程序包得什么文件删除了 现在提示配置文件错误 所以有这个程序包得 ...
- 使用matplotlib的示例:调整字体-设置colormap和colorbar
使用matplotlib的示例:调整字体-设置colormap和colorbar # -*- coding: utf-8 -*- #********************************** ...
- Android intent action大全
android.intent.action.ALL_APPSandroid.intent.action.ANSWERandroid.intent.action.ATTACH_DATAandroid.i ...
- rac 10g 10.2.0.1升级到10.2.0.5具体解释
RAC 10.2.0.1 升级到 10.2.0.5 一. 准备: Patch 包:p8202632_10205_LINUX.zip 节点数:3个节点 RAC1 RAC2 ...
- 用最简单的例子理解策略模式(Strategy Pattern)
当一个动作有多种实现方法,在实际使用时,需要根据不同情况选择某个方法执行动作,就可以考虑使用策略模式. 把动作抽象成接口,比如把玩球抽象成接口. public interface IBall { vo ...