[python 学习] 使用 xml.etree.ElementTree 模块处理 XML
---恢复内容开始---
导入数据(读文件和读字符串)
本地文件 country_data.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
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
从字符串中读取 xml
root = ET.fromstring(country_data_as_string)
Element.tag 、Element.text 和 Element.attributes
# root.tag 读取 tag 名
# root.attrib 读取 attributes
# root[0][1].rank 读取文本值
print root.tag #输出根节点元素<data>的tag名:data
print root.attrib #输出根节点元素<data>的attributes(空值):{}
print root[0][2].text #输出第一个<country>的子元素<gdppc>的文本值:141100

Element.iter()
# root.iter('neighbor')查找所有子孙元素
for neighbor in root.iter('neighbor'):
print neighbor.attrib

find() 和 findall()
# find()返回第一个子元素
print root.find('country')
# fund() 返回所有子元素
print root.findall('country')

照搬手册:
https://docs.python.org/2/library/xml.etree.elementtree.html
[python 学习] 使用 xml.etree.ElementTree 模块处理 XML的更多相关文章
- python xml.etree.ElementTree模块
使用的XML文件如下:file.xml <?xml version="1.0"?> <data name="ming"> <cou ...
- xml.etree.ElementTree模块的封装
转载:https://www.cnblogs.com/hongten/p/hongten_python_xml_etree_elementtree.html 1 # -*- coding: utf-8 ...
- python xml.etree ElementTree解析 编辑 xml
python有很多种xml解析方式,不过感觉etree的ElementTree 用起来最方便. #coding=utf-8 from xml.etree import ElementTree impo ...
- 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.etree.ElementTree读写xml文件实例
import osimport xml.etree.ElementTree as ET'''Python 标准库中,提供了6种可以用于处理XML的包,本文举实例说明第6种1.xml.dom2.xml. ...
- python模块:xml.etree.ElementTree
"""Lightweight XML support for Python. XML is an inherently hierarchical data format, ...
- python模块之xml.etree.ElementTree
xml.etree.ElementTree用于解析和构建XML文件 <?xml version="1.0"?> <data> <country nam ...
- Python学习第十二课——json&pickle&XML模块&OS模块
json模块 import json dic={'name':'hanhan'} i=8 s='hello' l=[11,22] data=json.dumps(dic) #json.dumps() ...
随机推荐
- justify-content
CSS3弹性布局内容对齐(justify-content)属性使用详解 内容对齐(justify-content)属性应用在弹性容器上,把弹性项沿着弹性容器的主轴线(main axis)对齐. 该操作 ...
- WCF - Hosting WCF Service 四种托管方式
https://www.tutorialspoint.com/wcf/wcf_hosting_service.htm After creating a WCF service, the next st ...
- Linux驱动开发5——同步机制
上一章讲到了并发,指的是多个进程同时存取临界区资源的处理机制.这一章讲的同步机制,讲的是多个进程之间协同工作的处理机制,如临界区数据还没有准备好,A进程负责准备数据,B进程等待A进程完成之后读取数据. ...
- ORACLE异机增量备份恢复
PROD异机增量备份恢复验证实施文档 准备工作:source 源库:PROD数据库备份策略:周日0级RMAN备份,周一至周六1级差异增量备份0 4 * * 0 /data/rmanlev0.sh &g ...
- JS new date在IOS出现的问题
实例代码: input = input.replace(/\-/g, "/");//横杠的时间不能被识别,所以要替换程斜杠 let time = new Date(input); ...
- vue +ts 在router的路由中import报错的解决方案
在router.ts中引入.vue文件,会提示打不到module,但是编译可能成功,运行也不报错 找了好久,发现了这个答案 https://segmentfault.com/a/11900000167 ...
- leetcode 40. 组合总和 II (python)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- 测开之路一百零四:jquery操作样式
jquery操作样式 添加样式.删除样式 切换样式 css("属性","值") css("属性","值"), 修改多个 ...
- 关于一段有趣代码引出的String创建对象的解释
通常来说,我们认为hashCode不相同就为不同的对象.就这样由一段代码引发了一场讨论,代码如下: @Test public void stringCompare() { String s1 = &q ...
- Golang闭包的坑
team leader 发现一个Golang程序的bug,是由不正确使用闭包引起.记载一下,以作备忘. 猜猜一下程序的结果: import ( "fmt" "time&q ...