[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() ...
随机推荐
- Linux中的NetworkManager网络管理
转载关于 NM_CONTROLLED和Network Manager Redhat在RHEL 6(Redhat Enterprise Linux),上搞了一个 Network manger 服务(同样 ...
- 10.1 ‘The server's host key is not cached in the registry’
10.1 ‘The server's host key is not cached in the registry’ This error message occurs when PuTTY conn ...
- KindEditor上传图片一直提示undefined
图片已经上传成功了,但是就是不在文本编辑器里显示图片,一直弹出undefined 返回的JSON都对呀!这是官网说的返回值: //成功时 { "error" : 0, " ...
- malloc函数分配内存失败的常见原因
malloc()函数分配内存失败的常见原因: 1. 内存不足. 2. 在前面的程序中出现了内存的越界访问,导致malloc()分配函数所涉及的一些信息被破坏.下次再使用malloc()函数申请内存 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_06 Set集合_5_HashSet存储自定义类型元素
想存储的元素不重复,就必须重写hashCode和equals这两个方法 新建一个Person类.添加姓名和年龄这两个成员变量..get和set,有参和无参构造. 重点是重写了toString的方法 自 ...
- 封装redis(set/get/delete)str和哈希类型
将Redis的常用操作封装了一下: import redis class MyRedis(): def __init__(self,ip,passwd,port=6379,db=0): #构造函数 t ...
- Vue Router 路由守卫:完整的导航解析流程
完整的导航解析流程 1 导航被触发. 2 在失活的组件里调用离开守卫. 3 调用全局的 beforeEach 守卫. 4 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+). ...
- bootstrap-select控件全选,全不选,查询功能实现
先引入先在你的页面引入 bootstrap-select.css 和 bootstrap-select.js <link href="~/Content/plugins/bootstr ...
- 实验报告3&学习总结
1.已知字符串:"this is a test of java".按要求执行以下操作: 统计该字符串中字母s出现的次数. 统计该字符串中子串"is"出现的次数. ...
- [Web 前端] 018 css 清除浮动的四种方法
清除浮动的四种方法 加 clear: ...(见例1) 父级上增加属性 overflow:hidden(见例2.1) 在最后一个子元素的后面加一个空的 div,给它一个样式属性 clear: both ...