[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() ...
随机推荐
- 笔记本连接树莓派3b(不需要屏幕)
一.网线直连 工具:笔记本,网线,树莓派 软件:putty 过程: 将系统烧录进SD卡后,在root里添加一个名字为“ssh”的空白文件(不需后缀名)来开启ssh服务,SD卡里的cmdline.txt ...
- [CERC2016]:凸轮廓线Convex Contour(模拟+数学)
题目描述 一些几何图形整齐地在一个网格图上从左往右排成一列.它们占据了连续的一段横行,每个位置恰好一个几何图形.每个图形是以下的三种之一:$1.$一个恰好充满单个格子的正方形.$2.$一个内切于单个格 ...
- Java网络编程与NIO详解11:Tomcat中的Connector源码分析(NIO)
本文转载 https://www.javadoop.com 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.c ...
- C++ Boost库的编译及使用
https://www.jianshu.com/p/de1fda741beb https://www.cnblogs.com/weizhixiang/p/5804778.html Windows编译 ...
- 微信小程序 input 组件
输入框:该组件是原生组件,使用的时候要注意相关的设置 属性: value:类型 字符串 输入框的初始内容 type:类型 字符串 input类型 属性值:text 文本输入键盘 number 数字输入 ...
- 解决ios和Android的差异
第一个:input,button input标签在 android系统不带圆角,在ios系统上带圆角 解决办法: input,button{ -webkit-appearance:none; } 第二 ...
- 图论&线性基(?)(8.12)
边没有负权,最短路最多只有n条边 很暴力的思想: 先跑一遍最短路,找出最短路上的边,枚举每条边,翻倍,放进原图再跑一遍.取最大值 好熟悉啊 分层建图,建k层 每层内部是原图 若原图中u到v有连边,则由 ...
- git 还原、恢复、回退
通过git revert来实现线主干代码的回滚.如下命令 对于 merge类型的commit对象,还需要"-m"参数 git revert -m 1 commit-id 对于普通 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_03-常用API第二部分_第1节 Object类_1_Object类的toString方法
任何一个类都可以使用Object类中的方法 ## 1.1 概述 `java.lang.Object`类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终 ...
- dcef3 为按钮添加单击事件
http://ju.outofmemory.cn/entry/78083 dcef3 为按钮添加单击事件 BccSafe's Blog 2014-06-01 3706 阅读 DOM 演示dcef3操作 ...