Python模块——xml
xml模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,
不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。
xml的格式如下,就是通过<>节点来区别数据结构的:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<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中可以用以下模块操作xml
遍历
import xml.etree.ElementTree as ET
tree = ET.parse("xml test") # 打开xml文件
root = tree.getroot() # 得到根节点
# print(dir(root))
print(root.tag)
# 遍历xml文档
for child in root:
print('----------',child.tag, child.attrib) # 打印country节点
for i in child:
print(i.tag,i.text)
修改和删除xml文档内容
import xml.etree.ElementTree as ET
tree = ET.parse("xml_test")
root = tree.getroot() #f.seek(0)
# 修改
for node in root.iter('year'):
new_year = int(node.text) + 5
node.text = str(new_year) # 修改内容
node.set("attr_test","false")
tree.write('output.xml') # 写入文件
# #删除node
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('output.xml')
新建xml
import xml.etree.ElementTree as ET
root = ET.Element("namelist") # 创建root
name = ET.SubElement(root,"name",attrib={"enrolled":"yes"}) # 创建child--name
age = ET.SubElement(name,"age",attrib={"checked":"no"}) # 创建name child--age,sex,name
sex = ET.SubElement(name,"sex")
n = ET.SubElement(name,"name")
n.text = "Alex Li"
sex.text = 'male'
name2 = ET.SubElement(root,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = ''
et = ET.ElementTree(root) # 生成文档对象
et.write("build_out.xml", encoding="utf-8",xml_declaration=True)
由于原生保存的XML时默认无缩进,如果想要设置缩进的话, 需要修改保存方式
import xml.etree.ElementTree as ET
from xml.dom import minidom def subElement(root, tag, text):
ele = ET.SubElement(root, tag)
ele.text = text def saveXML(root, filename, indent="\t", newl="\n", encoding="utf-8"):
rawText = ET.tostring(root)
dom = minidom.parseString(rawText)
with open(filename, 'w') as f:
dom.writexml(f, "", indent, newl, encoding) root = ET.Element("namelist") to = root.makeelement("to", {})
to.text = "peter"
root.append(to) name = ET.SubElement(root,"name",attrib={"enrolled":"yes"}) # 创建child--name
age = ET.SubElement(name,"age",attrib={"checked":"no"}) # 创建name child--age,sex,name
sex = ET.SubElement(name,"sex")
n = ET.SubElement(name,"name")
n.text = "Alex Li"
sex.text = 'male' name2 = ET.SubElement(root,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '' # et = ET.ElementTree(root) # 生成文档对象 # 保存xml文件
saveXML(root, "note.xml")
Python模块——xml的更多相关文章
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
- Python模块-xml
XML的例子 import xml.etree.ElementTree as ET tree = ET.parse("test.xml") root = tree.getroot( ...
- python操作xml文件
一.什么是xml? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. abc.xml <?xml version="1.0&q ...
- python模块(shelve,xml,configparser,hashlib,logging)
1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...
- python解析xml模块封装代码
在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
- 小白的Python之路 day5 模块XML特点和用法
模块XML的特点和用法 一.简介 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今 ...
- python configparse模块&xml模块
configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...
- python模块之xml
xml模块 xml结构 xml是种实现不同语言或程序之间进行数据交换的协议,跟json差不多,但没json使用简单.但是因为历史遗留问题,至今很多行业依然使用xml这种数据格式. xml的格式如下,是 ...
随机推荐
- SpringMVC处理XML格式的数据
1.搭建SpringMVC+spring环境 2.web.xml,Springmvc-config.xml.springMVC提供了处理xml格式请求响应的HttpMessageConverter,s ...
- m_sequencer、p_sequencer
https://blog.csdn.net/zhajio/article/details/79608323 p - parent sequencer - 要处理的实际sequencer类型的句柄,这个 ...
- 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015)
题目链接 : http://codeforces.com/gym/100781/attachments A-Adjoin the Network 题意大概是有多棵树(也可能是一颗),现在要把它们合并 ...
- 测验2: Python基础语法(上) (第4周)
快乐的数字 描述 编写一个算法来确定一个数字是否“快乐”. 快乐的数字按照如下方式确定:从一个正整数开始,用其每位数的平方之和取代该数,并重复这个过程,直到最后数字要么收敛等于1且一直等于1,要么将无 ...
- 31. Next Permutation 返回下一个pumutation序列
[抄题]: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...
- Linux学习小记(1)
学习Linux,进行阶段性总结,权当笔记方便日后翻阅和查看. 在此特别推荐peida的博客,他的有关Linux的理解个人感觉深入浅出,很适合入门的小白来理解和学习. 说一说针对IP的修改,ifconf ...
- linux学习第九天 (Linux就该这么学)
今天讲了raid0 至少两块盘串联在一起,读写性能提升,但不具备数据备份和错误修复能力,RAID1把两块盘绑定,在写入数据时,同时写入到多块硬盘设备,raid5推荐使用,10推荐使用 LVM,今天是 ...
- Python11/12--GIL/互斥锁/进程池
GIL1.全局解释器锁? 锁就是线程里面那个锁 锁是为了避免资源竞争造成数据的错乱 2.python程序的执行过程? 1.启动解释器进程 python.exe 2.解析你的py文件并执行它 每个py程 ...
- 2019.02.14 codechef Chef at the Food Fair(线段树+泰勒展开)
传送门 题意:现在有nnn个位置,每个位置上有一个值aia_iai. 要求支持如下两种操作: 区间乘vvv 求区间的(1−ai)(1-a_i)(1−ai)之积 思路: 考虑转换式子: Ans=∏i ...
- CentOs7安装PHP
来源:https://jingyan.baidu.com/article/2d5afd692df18d85a3e28e4c.html 一.下载源码 1.cd /usr/local/src/ 2. 执行 ...