[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() ...
随机推荐
- HTML&&CSS基础知识点整理
HTML&&CSS基础知识点整理 一.WEB标准:一系列标准的集合 1. 结构(Structure):html 语言:XHTML[可扩展超文本标识语言]和XML[可扩展标记语言] 2. ...
- ES5 Object.defineProperties / Object.defineProperty 的使用
临时笔记,稍后整理 var obj = { v: , render: function () { console.log(") } }; // Object.defineProperties ...
- 【原创】基于phpGrace+uniApp开发之:5.登录界面增加图片验证码
1.目的: 采用phpGrace中的图片验证码,在用户名+密码登录时使用图片验证码进行验证. 2.文档地址: 图片验证码的文档地址:http://www.phpgrace.com/tools/info ...
- MDX 入门
之前用到的SQL,解释:结构化查询语言(Structured Query Language)(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言,是一种 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_5_BufferedWriter_字符缓冲输出流
使用newLine来换行 同样的效果 println的源码里面其实就用的就是newLine()
- django在线教育网站开发---需求分析
开发目录: django app设计 user model.py 编写 courses models.py 编写 -Course 课程基本信息 -Lesson 章节信息 -Video -视频 -Cou ...
- 【ABAP系列】SAP ABAP诠释BDC的OK CODE含义
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP诠释BDC的OK ...
- TensorFlow学习笔记8-深度学习的正则化
深度学习的正则化 回顾一些基本概念 概念 描述 设计矩阵 数据集在特征向量上的表示 训练误差 学习到的模型与训练集标签之间的误差 泛化误差(测试误差) 学习到的模型与测试集之间的误差 欠拟合 模型的训 ...
- NOPI导入导出EXCEL
一.简介 1. 什么是NPOI NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office 97 ...
- ios平台appstore不支持网页内嵌app解决方案
苹果一直拒绝 UIWebView 内嵌 HTML5 页面的 iPhone.iPad APP应用上架到 App Store,建议这样的APP去做成Safari的Web应用.但是,苹果的审核人员只从界面. ...