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的格式如下,是 ...
随机推荐
- Unity 2018 By Example 2nd Edition
Unity is the most exciting and popular engine used for developing games. With its 2018 release, Unit ...
- springboot自带定时任务和集成quartz
1,springboot自带的定时任务 默认是单线程 有这个依赖就可以 <dependency> <groupId>org.springframework.boot</ ...
- entity framework core 生成 postgresql 数据库实体
.net core 2.0 使用db first 方式生成 表 和context PM 控制台运行命令出错 Scaffold-DbContext "Host=localhost;Databa ...
- C++字符串结束标识
用一个字符数组可以存放一个字符串中的字符.如: char str[12]={'I',' ','a','m',' ','h','a','p','p','y'}; 用一维字符数组str来存放一个字符串″I ...
- Spring 配置文件中 元素 属性 说明
<beans /> 元素 该元素是根元素.<bean /> 元素的属性 default-init // 是否开启懒加载.默认为 false default-dependency ...
- PowerShell 脚本中调用密文密码
1. 把密码转变为加密的字符串.使用命令 ConvertFrom-SecureString Read-Host "Enter Password" -AsSecureString | ...
- idea环境配置
Idea 下载 https://www.jetbrains.com/idea/download/#section=windows idea安装(略) idea破解 window配置hosts文件:0. ...
- POI 导出
package com.baoqilai.scp.service; import java.io.File; import java.io.FileOutputStream; import java. ...
- 巧用 Jersey RESTful WebService框架解决文件上传乱码
一.当我们使用jersey框架封装的restful进行文件上传时,会出现中文乱码,试用了过滤器设置编码都不管用.仔细想了很久解决办法,就用一个servelt来代替这个上传的restful接口实现上传的 ...
- MySQL数据库插入中文乱码解决方法
在mysql数据库中,插入中文数据时,会出现乱码的现象. 我的测试方法: 首先用Navicat for MySql 插入一行数据,带有中文的. 再用mysql命令行来查看插入的数据,看是否出现乱码. ...