xml模块
描述:xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但是Json使用起来更简单,json还没有诞生时,xml已经开始使用很久,至今很多传统公司如金融行业很多系统的接口还是主要使用xml

存储数据的格式:一个个的标签组成

[root@python3 xml]# cat test.xml
<?xml version="1.0"?>
<data><country name="Liechtenstein">                   #国家,country是标签,name是它的属性<rank updated="yes">2</rank>                 #排名<year>2008</year>                            #年份<gdppc>141100</gdppc>                        #gdp<neighbor name="Austria" direction="E"/>     #邻国<neighbor name="Switzerland" direction="W"/> #邻国,这种写法是自闭合的写法</country><country name="Singapore"><rank updated="yes">5</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank updated="yes">69</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country>
</data>

  

xml在各个语言中都支持,以下是在python中的使用

[root@python3 xml]# cat py_xml.py
#!/usr/local/python3/bin/python3
import xml.etree.ElementTree as ETtree = ET.parse("test.xml")   #解析那个xml的数据
root = tree.getroot()         #得到最外层标签data
print(root.tag)

遍历xml文档

for child in root:            #root是最外层,其他是它的子标签print(child.tag, child.attrib)  #标签名,属性打印for i in child:print(i.tag, i.text)        #子下还有标签,文本

只遍历year节点

for node in root.iter('year'):print(node.tag, node.text)

root的内容

[root@python3 xml]# cat py_xml.py
#!/usr/local/python3/bin/python3
import xml.etree.ElementTree as ETtree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
[root@python3 xml]# python3 py_xml.py
data

打印国家

[root@python3 xml]# cat py_xml.py
#!/usr/local/python3/bin/python3
import xml.etree.ElementTree as ETtree = ET.parse("test.xml")
root = tree.getroot()
print(root.tag)for child in root:print(child.tag, child.attrib)
[root@python3 xml]# python3 py_xml.py
data
country {'name': 'Liechtenstein'}  #country: child.tag, {'name': 'Liechtenstein'}是child.attrib
country {'name': 'Singapore'}
country {'name': 'Panama'}

打印文本

[root@python3 xml]# cat py_xml.py
#!/usr/local/python3/bin/python3
import xml.etree.ElementTree as ETtree = ET.parse("test.xml")
root = tree.getroot()
print(root.tag)for child in root:print(child.tag, child.attrib)for i in child:print(i.tag, i.text)
[root@python3 xml]# python3 py_xml.py
data
country {'name': 'Liechtenstein'} #国家下的文本
rank 2                            #rank: i.tag   2: i.text
year 2008
gdppc 141100
neighbor None                     #neighbor是自定义的标签没有自己的文本内容
neighbor Nonecountry {'name': 'Singapore'}
rank 5
year 2011
gdppc 59900
neighbor Nonecountry {'name': 'Panama'}
rank 69
year 2011
gdppc 13600
neighbor None
neighbor None

  

xml的修改

[root@python3 xml]# cat py_mod_xml.py
#!/usr/local/python3/bin/python3
import xml.etree.ElementTree as ETtree = ET.parse("test.xml")
root = tree.getroot()for node in root.iter('year'):    #对root下year的标签做循环,node是year的标签,year标签中有text(是那个数字,如2011)new_year = int(node.text) + 1 #对年份的份做修改,处理下,加1,变成新的值,如2011+1 = 2012node.text = str(new_year)     #再转换成字符串,再转换回它的node.textnode.set("updated","yes")     #还有一个updated的属性,使用set来修改tree.write("test1.xml")            #修改完后都在tree对象里,再使用tree调用write方法重新写[root@python3 xml]# python3 py_mod_xml.py
[root@python3 xml]# cat test1.xml
<data><country name="Liechtenstein"><rank updated="yes">2</rank><year updated="yes">2009</year>    #修改了,重新加一个属性<gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Singapore"><rank updated="yes">5</rank><year updated="yes">2012</year>    ###<gdppc>59900</gdppc><neighbor direction="N" name="Malaysia" /></country><country name="Panama"><rank updated="yes">69</rank><year updated="yes">2012</year>        ####<gdppc>13600</gdppc><neighbor direction="W" name="Costa Rica" /><neighbor direction="E" name="Colombia" /></country>

  

xml的删除操作

[root@python3 xml]# cat py_del_xml.py
#!/usr/local/python3/bin/python3
import xml.etree.ElementTree as ETtree = ET.parse("test.xml")
root = tree.getroot()for country in root.findall('country'):        #遍历所有国家rank = int(country.find('rank').text)      #找到排名的标签,取到text(排名的数字)if rank > 50:                               #排名大于50的移除root.remove(country)
tree.write('test2.xml')   [root@python3 xml]# python3 py_del_xml.py
[root@python3 xml]# cat test2.xml
<data><country name="Liechtenstein"><rank updated="yes">2</rank><year>2008</year><gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Singapore"><rank updated="yes">5</rank><year>2011</year><gdppc>59900</gdppc><neighbor direction="N" name="Malaysia" /></country></data>[root@python3 xml]#

  

自创建xml

[root@python3 xml]# vim py_create_xml.py
#!/usr/local/python3/bin/python3
import xml.etree.ElementTree as ETnew_xml = ET.Element("namelist")                                  #ET调用Element方法
name1 = ET.SubElement(new_xml,"name", attrib={"enrolled": "yes"})  #ET调用SubElement方法,和属性
age = ET.SubElement(name1, "age", attrib={"checked": "no"})
sex = ET.SubElement(name1, "sex")
age.text = '33'
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '19'et = ET.ElementTree(new_xml)                                    #生成文档对象
et.write("test3.xml", encoding="utf-8", xml_declaration=True)
ET.dump(new_xml)                                                #打印生成格式
[root@python3 xml]# python3 py_create_xml.py
<namelist><name enrolled="yes"><age checked="no">33</age><sex /></name><name enrolled="no"><age>19</age></name></namelist>

 

转载于:https://www.cnblogs.com/reid21/articles/8855990.html

Python攻克之路-xml模块相关推荐

  1. Python攻克之路-random模块

    random模块 描述:生成随机数 random常用方法 random In [2]: random.random() #0-1之间 Out[2]: 0.2295625620781645 randin ...

  2. Python攻克之路-hashlib模块

    hashlib模块 描述:加密模块,从明文加密成密文,主要是md5和sha md5 In [13]: import hashlib In [14]: m=hashlib.md5() In [15]: ...

  3. python常用内置模块-Python常用内置模块之xml模块

    xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,超文本标记语言被设计用来显示 ...

  4. python常用内置模块-Python常用内置模块之xml模块(详解)

    xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,超文本标记语言被设计用来显示 ...

  5. Python攻克之路-网络编程(文件上传实现思路)

    需求:一个server,一个client,实现client把某个文件传到server中某个目录中 分析:实际是实现数据传输,设定一个命令和一个参数(上传的内容),连接后,让用户输入命令和要传送的内容, ...

  6. Python攻克之路-高阶函数

    高阶函数 在数学和计算机科学中,高阶函数是至少满足下列一个条件的函数: 接受一个或多个函数作为输入 输出一个函数 在数学中它们也叫做算子(运算符)或泛函.微积分中的导数就是常见的例子,因为它映射一个函 ...

  7. python英译汉库模块_Python 进阶之路-翻译模块

    Python 进阶之路-翻译模块 作者:nango  阅读:1749次  来源:原创  时间:2017-07-14 17:09 Git 地址 [python 翻译模块](https://github. ...

  8. Python之路(第十六篇)xml模块、datetime模块

    一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...

  9. python中常用的序列化模块_第六章 常用模块(5):python常用模块(序列化模块:pickle,json,shelve,xml)...

    6.3.7 序列化模块 (pickle,json,shelve,xml) 文件写入,数据传输时,我们都是以字符串形式写入的(因为字符串可以encode成bytes). 那其他类型(比如字典,列表等)想 ...

最新文章

  1. php和python哪个工资高-Java、Python、Php学哪个好,哪个更有前景?
  2. bootstrap-关闭按钮
  3. eayUi panel实现上一页下一页
  4. Yolov1-手把手用自己的数据集训练自己的模型
  5. 微信小程序接入腾讯地图sdk地图 用户自选位置。踩坑+代码实现
  6. json 服务器 文件,json属于服务器文件吗
  7. Swift傻傻分不清楚系列(十)枚举
  8. 3千内!苹果最便宜iPhone稳了:坐等
  9. 我的成长笔记20210330(测试空闲期)
  10. UVA12148 LA4214 Electricity【日期计算】
  11. python必备基础代码-机器学习算法基础(使用Python代码)
  12. 集成学习之Adaboost算法
  13. 学习Android逆向
  14. Ubuntu各类软件推荐
  15. mysql 正则表达式 标点符号_正则表达式-匹配标点符号
  16. 转:饭后九不要包你保健康
  17. linux generic netlink实现机制:注册、创建
  18. 2018美团点评编程题第一题
  19. 华为OD机试 - 日志首次上报最多积分
  20. Spring框架中的单例Beans是线程安全的么

热门文章

  1. 【深度优先搜索】网格类问题:牛客网:机器人的运动范围
  2. 简单使用linux感受,linux小白说说用linux的感受
  3. matlab教程曲线拟合,matlab 曲线拟合
  4. linux检查哪些进程消耗io,Linux 不同方法查看进程消耗CPU IO 等
  5. DBUtils注意问题
  6. height、clientHeight、scrollHeight、offsetHeight区别
  7. 【CCCC】L2-014 列车调度 (25分),贪心,set维护序列
  8. mysql 判断日志时间早_MySQL5.7慢查询日志时间与系统时间差8小时原因详解
  9. commons-fileupload的ServletFileUpload类
  10. 枚举如何设置空白_2019-07-04 用VBA设置word中shape对象相对位置