python模块之xml.etree.ElementTree
xml.etree.ElementTree用于解析和构建XML文件
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
解析XML文件
parse()函数,从xml文件返回ElementTree
from xml.etree.ElementTree import parse
tree = parse('demo.xml') //获取ElementTree
root = tree.getroot() // 获取根元素
Element.tag 、Element.attrib、Element.text
In [6]: root.tag
Out[6]: 'data' In [7]: root.attrib
Out[7]: {} In [25]: root.text
Out[25]: '\n '
for child in root 迭代获得子元素
In [8]: for child in root:
...: print(child.tag, child.attrib)
...:
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
Element.get() 获得属性值
In [27]: for child in root:
...: print (child.tag, child.get('name'))
...:
country Liechtenstein
country Singapore
country Panama
root.getchildren() 获得直接子元素
In [21]: root.getchildren()
Out[21]:
[<Element 'country' at 0x7f673581c728>,
<Element 'country' at 0x7f673581ca98>,
<Element 'country' at 0x7f673581cc28>]
root[0][1] 根据索引查找子元素
In [9]: root[0][1].text
Out[9]: '2008' In [10]: root[1][0].text
Out[10]: '4'
root.find() 根据tag查找直接子元素,返回查到的第一个元素
In [13]: root.find('country').attrib
Out[13]: {'name': 'Liechtenstein'}
root.findall() 根据tag查找直接子元素,返回查到的所有元素的列表
In [16]: for country in root.findall('country'):
...: print (country.attrib)
...:
{'name': 'Liechtenstein'}
{'name': 'Singapore'}
{'name': 'Panama'}
root.iterfind() 根据tag查找直接子元素,返回查到的所有元素的生成器
In [22]: root.iterfind('country')
Out[22]: <generator object prepare_child.<locals>.select at 0x7f6736dccfc0>
支持的XPath语句(XML Path)
In [19]: root.findall('.//rank') //查找任意层次元素
Out[19]:
[<Element 'rank' at 0x7f673581c8b8>,
<Element 'rank' at 0x7f673581c6d8>,
<Element 'rank' at 0x7f673581cc78>] In [32]: root.findall('country/*') //查找孙子节点元素
Out[32]:
[<Element 'rank' at 0x7f673581c8b8>,
<Element 'year' at 0x7f673581cbd8>,
<Element 'gdppc' at 0x7f673581c958>,
<Element 'neighbor' at 0x7f673581c688>,
<Element 'neighbor' at 0x7f673581cb38>,
<Element 'rank' at 0x7f673581c6d8>,
<Element 'year' at 0x7f673581c5e8>,
<Element 'gdppc' at 0x7f673581c868>,
<Element 'neighbor' at 0x7f673581cb88>,
<Element 'rank' at 0x7f673581cc78>,
<Element 'year' at 0x7f673581ccc8>,
<Element 'gdppc' at 0x7f673581cd18>,
<Element 'neighbor' at 0x7f673581cd68>,
<Element 'neighbor' at 0x7f673581cdb8>] In [33]: root.findall('.//rank/..') // ..表示父元素
Out[33]:
[<Element 'country' at 0x7f673581c728>,
<Element 'country' at 0x7f673581ca98>,
<Element 'country' at 0x7f673581cc28>] In [34]: root.findall('country[@name]') // 包含name属性的country
Out[34]:
[<Element 'country' at 0x7f673581c728>,
<Element 'country' at 0x7f673581ca98>,
<Element 'country' at 0x7f673581cc28>] In [35]: root.findall('country[@name="Singapore"]') // name属性为Singapore的country
Out[35]: [<Element 'country' at 0x7f673581ca98>] In [36]: root.findall('country[rank]') // 孩子元素中包含rank的country
Out[36]:
[<Element 'country' at 0x7f673581c728>,
<Element 'country' at 0x7f673581ca98>,
<Element 'country' at 0x7f673581cc28>] In [37]: root.findall('country[rank="68"]') // 孩子元素中包含rank且rank元素的text为68的country
Out[37]: [<Element 'country' at 0x7f673581cc28>] In [38]: root.findall('country[1]') // 第一个country
Out[38]: [<Element 'country' at 0x7f673581c728>] In [39]: root.findall('country[last()]') // 最后一个country
Out[39]: [<Element 'country' at 0x7f673581cc28>] In [40]: root.findall('country[last()-1]') // 倒数第二个country
Out[40]: [<Element 'country' at 0x7f673581ca98>]
root.iter() 递归查询指定的或所有子元素
In [29]: root.iter()
Out[29]: <_elementtree._element_iterator at 0x7f67355dd728> In [30]: list(root.iter())
Out[30]:
[<Element 'data' at 0x7f673581c778>,
<Element 'country' at 0x7f673581c728>,
<Element 'rank' at 0x7f673581c8b8>,
<Element 'year' at 0x7f673581cbd8>,
<Element 'gdppc' at 0x7f673581c958>,
<Element 'neighbor' at 0x7f673581c688>,
<Element 'neighbor' at 0x7f673581cb38>,
<Element 'country' at 0x7f673581ca98>,
<Element 'rank' at 0x7f673581c6d8>,
<Element 'year' at 0x7f673581c5e8>,
<Element 'gdppc' at 0x7f673581c868>,
<Element 'neighbor' at 0x7f673581cb88>,
<Element 'country' at 0x7f673581cc28>,
<Element 'rank' at 0x7f673581cc78>,
<Element 'year' at 0x7f673581ccc8>,
<Element 'gdppc' at 0x7f673581cd18>,
<Element 'neighbor' at 0x7f673581cd68>,
<Element 'neighbor' at 0x7f673581cdb8>] In [31]: list(root.iter('rank'))
Out[31]:
[<Element 'rank' at 0x7f673581c8b8>,
<Element 'rank' at 0x7f673581c6d8>,
<Element 'rank' at 0x7f673581cc78>]
python模块之xml.etree.ElementTree的更多相关文章
- python模块:xml.etree.ElementTree
"""Lightweight XML support for Python. XML is an inherently hierarchical data format, ...
- python标准库xml.etree.ElementTree的bug
使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...
- [python 学习] 使用 xml.etree.ElementTree 模块处理 XML
---恢复内容开始--- 导入数据(读文件和读字符串) 本地文件 country_data.xml <?xml version="1.0"?> <data> ...
- [python 2.x] xml.etree.ElementTree module
XML 文件:xmlparse.xml <?xml version="1.0" encoding="UTF-8" standalone="no& ...
- Python 标准库之 xml.etree.ElementTree
Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...
- python 之xml.etree.ElementTree
Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. [注意]xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. 每个element对象都具有以下属性: ...
- python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用
1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...
- python xml.etree.ElementTree模块
使用的XML文件如下:file.xml <?xml version="1.0"?> <data name="ming"> <cou ...
- python 解析xml遇到xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 4, column 34
在调试数字驱动用xml文件的方式时,包含读取xml文件的步骤,运行程序报错: d:\test\0629>python XmlUtil.pyTraceback (most recent call ...
随机推荐
- 题解 P1200 【[USACO1.1]你的飞碟在这儿Your Ride Is He…】
cin其中有很多小众的函数与其他重叠 不妨拿来用用(作死不止) 划重点!!! 1.cin.get(),相当于c里面的getchar(),可以往里面输入字符 2.cin.getline(),相当于str ...
- Android ActionBar 使用详解
ActionBar取代了以前的TitleBar,是一种更加灵活的人机交互方式:ActionBar并不是完全自立门户的一个新兴的东西,而是和3.0以下版本的menu进行了合并整合:so,添加action ...
- 【51Nod1258】序列求和V4(FFT)
[51Nod1258]序列求和V4(FFT) 题面 51Nod 多组数据,求: \[Ans=\sum_{i=1}^ni^k,n\le 10^{18},k\le50000\] 题解 预处理伯努利数,时间 ...
- 洛谷P1242 新汉诺塔 【神奇的递归】
题目描述 设有n个大小不等的中空圆盘,按从小到大的顺序从1到n编号.将这n个圆盘任意的迭套在三根立柱上,立柱的编号分别为A.B.C,这个状态称为初始状态. 现在要求找到一种步数最少的移动方案,使得从初 ...
- redis分布式(主从复制)
Redis主从复制配置和使用都非常简单.通过主从复制可以允许多个slave server拥有和master server相同的数据库副本. Redis的复制原理:本身就是Master发送数据给s ...
- yd的拔钉子之路之 POI 2017
写在前面的一些话 如果我NOIP没退役,这大概会写成一个系列吧,所以这算是系列的开始,要写一些奇怪的东西? 首先解释下什么叫“拔钉子”,其实就是在钉子上做题嘛......至于钉子具体是个什么东西就当面 ...
- python基础----迭代器、生成器、协程函数及应用(面向过程实例)
一.什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2.可迭代 ...
- 服务器上的 Git - 生成 SSH 公钥
http://git-scm.com/book/zh/ch4-3.html 生成 SSH 公钥 如前所述,许多 Git 服务器都使用 SSH 公钥进行认证. 为了向 Git 服务器提供 SSH 公钥, ...
- 「Linux」centos7安装uWSGI
一定要记得先安装python-devel,再安装uWSGI,否则即使安装成功也是不能使用的,切记切记
- [Java多线程]-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...