python学习-52 XML模块
XML模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但是json使用起来更简单。
例如:创建一个xml文件
<data>
<country name="china">
<rank updated="yes">1</rank>
<year>2019</year>
<neighbor name="American"/>
</country>
<country name="Canada">
<rank updated="yes">2</rank>
<year>2019</year>
<neighbor name="American"/>
</country>
</data>
查询内容
import xml.etree.ElementTree as ET
tree = ET.parse("xml")
root = tree.getroot()
print(root.tag)
print('__________________')
# 查
for a in root: # 遍历根
print(a.tag)
print(a.attrib)
for b in a : # 遍历根的下一级
print(b.text)
for node in root.iter('year'): #遍历节点,取year的内容
print(node.tag,node.text)
运行结果:
data
__________________
country
{'name': 'china'}
2
2019
None
country
{'name': 'Canada'}
2
2019
None
year 2019
year 2019 Process finished with exit code 0
修改和删除
import xml.etree.ElementTree as ET
tree = ET.parse("xml")
root = tree.getroot()
# 修改
for node in root.iter("year"):
new_year = int(node.text) + 1 # 年份加1
node.text = str(new_year)
node.set("updated","yes") # 多加一个属性
tree.write("xml_text.xml") # 放到一个新的文件里
# 删除
for country in root.findall("country"):
rank = int(country.find("rank").text)
if rank >1: # 删除排名大于1的国家的信息
root.remove(country)
tree.write("xml_test2.xml")
创建一个xml文件
import xml.etree.ElementTree as ET
new_xml = ET.Element("namelist")
name =ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = ""
et = ET.ElementTree(new_xml)
et.write("xml2_test.xml",encoding="utf-8",xml_declaration=True)
python学习-52 XML模块的更多相关文章
- python学习之argparse模块
python学习之argparse模块 一.简介: argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行 ...
- Python学习day19-常用模块之re模块
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习 Part4:模块
Python学习 Part4:模块 1. 模块是将定义保存在一个文件中的方法,然后在脚本中或解释器的交互实例中使用.模块中的定义可以被导入到其他模块或者main模块. 模块就是一个包含Python定义 ...
- Python学习day18-常用模块之NumPy
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- python学习笔记_week5_模块
模块 一.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能), 本质就是.py结尾的python文件(文件名:test.py,对应模块名:test) 包:用来从逻辑上 ...
- Python学习-day5 常用模块
day5主要是各种常用模块的学习 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 conf ...
- Python学习笔记-常用模块
1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...
- Python学习笔记--XML的应用
XML的定义 XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没有被 ...
- python学习之random模块
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
随机推荐
- jQuery学习笔记——基本了解
安装 两种方法: 从 jquery.com 下载jQuery. 从 CDN 中载入 jQuery, 如从我的博客中加载 jQuery: <script src="https://blo ...
- Hadoop(一)—— 启动与基本使用
一.安装&启动 安装 下载hadoop2.7.2 https://archive.apache.org/dist/hadoop/common/hadoop-2.7.2/ 2.7.2-官方文档 ...
- JVM 数组创建的本质
1.创建数组 创建一个MyParent4[] 数组 public class MyTest4 { public static void main(String[] args) { MyParent4[ ...
- JVM常量的含义与反编译助记符详解
1.定义一个常量 public class MyTest2 { public static void main(String[] args) { System.out.println(MyParent ...
- arcpy arcgis python实例教程--原点夹角距离定义线(坐标正算)
arcpy arcgis python实例教程--原点夹角距离定义线(坐标正算) 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 此地理处理工具 ...
- linux: ubuntu 14.04 和16.04 快速下载
由于官网服务器在国外,下载速度奇慢,所以我们可以利用阿里云镜像下载ubuntuubuntu 14.04:http://mirrors.aliyun.com/ubuntu-releases/14.04/ ...
- Blockchain & BPM
http://www.infoq.com/cn/news/2018/07/blockchain-BPM?utm_source=notification_email&utm_campaign=n ...
- tomcat的AJP(定向包协议)协议
由于tomcat的html和图片解析功能相对其他服务器如apche等较弱,所以,一般都是集成起来使用,只有jsp和servlet服务交由tomcat处理,而tomcat和其他服务器的集成,就是通过aj ...
- Fiddler抓包工具版面认识(一)
Fiddler是一个蛮好用的抓包工具,可以将网络传输发送与接受的数据包进行截获.重发.编辑.转存等操作.也可以用来检测网络安全.反正好处多多,举之不尽呀!当年学习的时候也蛮费劲,一些蛮实用隐藏的小功能 ...
- 阿里重磅开源在线分析诊断工具Arthas(阿尔萨斯)
github地址: Arthas English version goes here. Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. 当你遇到以下类似问题而束手无策时,Art ...