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模块的更多相关文章

  1. python学习之argparse模块

    python学习之argparse模块 一.简介: argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行 ...

  2. Python学习day19-常用模块之re模块

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  3. Python学习 Part4:模块

    Python学习 Part4:模块 1. 模块是将定义保存在一个文件中的方法,然后在脚本中或解释器的交互实例中使用.模块中的定义可以被导入到其他模块或者main模块. 模块就是一个包含Python定义 ...

  4. Python学习day18-常用模块之NumPy

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. python学习笔记_week5_模块

    模块 一.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能), 本质就是.py结尾的python文件(文件名:test.py,对应模块名:test) 包:用来从逻辑上 ...

  6. Python学习-day5 常用模块

    day5主要是各种常用模块的学习 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 conf ...

  7. Python学习笔记-常用模块

    1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...

  8. Python学习笔记--XML的应用

    XML的定义 XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没有被 ...

  9. python学习之random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

随机推荐

  1. 【论文阅读】DCAN: Deep Contour-Aware Networks for Accurate Gland Segmentation

    DCAN: Deep Contour-Aware Networks for Accurate Gland Segmentation 作者:Hao Chen Xiaojuan Qi Lequan Yu ...

  2. Ubuntu16.04Apache负载均衡+集群

    mod_proxy ,主代理模块Apache模块用于重定向连接;它允许Apache充当底层应用程序服务器的网关.mod_proxy_http ,它增加了对代理HTTP连接的支持.mod_proxy_b ...

  3. python操作toml文件

    # -*- coding: utf-8 -*- # @Time : 2019-11-18 09:31 # @Author : cxa # @File : toml_demo.py # @Softwar ...

  4. Mysql 按年、季度、月、周查询统计

    User表 CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID', `username` varchar( ...

  5. 三大框架 之 Hibernate生成策略与缓存策略(主键生成策略、持久化、持久化类划分、一级缓存、事物管理)

    目录 Hibernate生成策略与缓存策略 主键生成策略 主键分类 主键的生成策略 持久化 什么是持久化 什么是持久化类 持久化类编写规则 持久化类的划分 三种状态区分 持久态对象特征 一级缓存 什么 ...

  6. 效率包括了代码的GC 大小与内存大小,执行速度等等。其中执行速度不是关注 的重点

    效率包括了代码的GC 大小与内存大小,执行速度等等.其中执行速度不是关注的重点

  7. include mime.types;

    include mime.types; (index):1 Resource interpreted as Stylesheet but transferred with MIME type appl ...

  8. SpringMVC RequestLoggingFilter log to file

    spring - How to Log HttpRequest and HttpResponse in a file? - Stack Overflowhttps://stackoverflow.co ...

  9. SSH SCP 远程密钥登录配置和服务器间的文件传输

    目录 ssh ssh是什么 ssh安装 使用ssh登录远程主机 退出登录 使用ssh执行单条指令 密钥验证 详细操作 scp rsync sftp 进阶 ssh ssh是什么 ssh (Secure ...

  10. Leetcode: Shortest Way to Form String

    From any string, we can form a subsequence of that string by deleting some number of characters (pos ...