XML模块

XML是实现不同语言或程序之间进行数据交换的协议,和json一样。

XML格式:

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

读XML

简单读取XML内容:

import xml.etree.ElementTree as ET
tree = ET.parse('xml_test.xml')
root = tree.getroot()
print(root) #XML对象
print(root.tag) #XML最外层标签

遍历XML:

import xml.etree.ElementTree as ET
tree = ET.parse('xml_test.xml')
root = tree.getroot() for child in root:
print(child.tag,child.attrib) #to {'name': 'to'} 遍历标签、属性
for i in child:
print(i.tag,i.text) #content Jim 遍历标签、文本
break
break

只遍历某一个标签:

for node in root.iter('content'):
print(node.tag,node.text) #content Jim #content Jani #content Reminder #content Don't forget me this weekend!

修改XML

for node in root.iter('content'):
content = node.text #获取内容
new_content = str('new_content_'+content)
node.text = new_content #修改内容
node.set('update','yes') #添加属性 tree.write('xml_test.xml') #保存到原文件则覆盖之前内容,保存到新文件则新建

删除XML

for node in root.findall('to'): #找到to这个标签
content = len(node.find('content').text) #继续找到content的标签,并且计算content内容长度
if content > 2:
root.remove(node) #移除这个标签
tree.write('xml_test3.xml') # 将结果重新写到文件

创建XML

import xml.etree.ElementTree as ET
new_xml = ET.Element("name_list") #创建节点
personinfo = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yes"}) #创建子节点、子节点名称、属性
age = ET.SubElement(personinfo,'age',attrib={"checked":"no"})
age.text = '33' #添加子节点文本内容
name = ET.SubElement(personinfo,"name")
name.text = 'jim' personinfo2 = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"no"})
age = ET.SubElement(personinfo2,'age')
age.text = '20'
name = ET.SubElement(personinfo2,'name')
name.text = 'Lily' et =ET.ElementTree(new_xml) #生成文档对象
et.write('xml_test5.xml',encoding='utf-8',xml_declaration=True) #生成xml文件并指定字符集和
ET.dump(new_xml) #打印生成格式

创建后的结果:

<?xml version='1.0' encoding='utf-8'?>
<name_list>
<personinfo enrolled="yes">
<age checked="no">33</age>
<name>jim</name>
</personinfo>
<personinfo enrolled="no">
<age>20</age>
<name>Lily</name>
</personinfo>
</name_list>

python模块详解 XML的更多相关文章

  1. python模块详解 | selenium(持续更新中)

    目录: 关于selenium Selenium 安装Selenium 安装浏览器驱动 配置环境变量 selenium方法详解 定位元素 元素操作 浏览器操作 鼠标事件 浏览器事件 设置元素等待 多表单 ...

  2. python模块详解 random os

    random模块 常用方法 random.random() 随机产生一个小于1的浮点数 import random print(random.random()) #0.4153761818276826 ...

  3. python模块详解

    什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...

  4. python模块详解 sys shutil

    sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sy ...

  5. python模块详解 | shutil

    简介: shutil是python的一个内置模块,提供了许多关于文件和文件集合的高级操作,特别提供文件夹与文件操作.归档操作了支持文件复制和删除的功能. 文件夹与文件操作: copyfileobj(f ...

  6. 小白的Python之路 day5 python模块详解及import本质

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

  7. Python 模块详解及import本质

    同在当前目录下的模块和包导入 模块定义 本质就是.py结尾的python文件. 用来从逻辑上组织python代码(变量,函数,类,逻辑) 文件名: test.py;  对应的模块名 : test 模块 ...

  8. Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)

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

  9. python模块详解 logging

    打印日志的五个级别: import logging logging.debug('test debug') logging.info('test info') logging.warning('tes ...

随机推荐

  1. FPGA实战操作(1) -- SDRAM(操作说明)

    SDRAM是做嵌入式系统中,常用是的缓存数据的器件.基本概念如下(注意区分几个主要常见存储器之间的差异): SDRAM(Synchronous Dynamic Random Access Memory ...

  2. javascript 获取当前 URL 参数的两种方法

    window.location.host; //返回url 的主机部分,例如:www.xxx.com window.location.hostname; //返回www.xxx.com window. ...

  3. Codeforces-B-Game with string(模拟栈)

    Two people are playing a game with a string ss, consisting of lowercase latin letters. On a player's ...

  4. codeforces-777E Hanoi Factory (栈+贪心)

    题目传送门 题目大意: 现在一共有N个零件,如果存在:bi>=bj&&bj>ai的两个零件i,j,那么此时我们就可以将零件j放在零件i上.我们现在要组成一个大零件,使得高度 ...

  5. Avito Cool Challenge 2018:D. Maximum Distance

    D. Maximum Distance 题目链接:https://codeforces.com/contest/1081/problem/D 题意: 给出一个连通图以及一些特殊点,现在定义cost(u ...

  6. 移动端的click点透问题

    在移动端开发中,有时会出现click点透的问题. 一.什么是click点透 以下情况,在B元素上有半透明红色遮盖层A,黄色B元素内有可点击链接C. tips:以下举例仅针对webkit内核浏览器,所有 ...

  7. select获取到option的value和text方法

    function getSelectval(id){ var selId = document.getElementById(id); //获取select的id var seleIndex =sel ...

  8. jemeter+badboy录制脚本

    Jmeter 是一个非常流行的性能测试工具,虽然与LoadRunner相比有很多不足,比如:它结果分析能力没有LoadRunner详细:很它的优点也有很多: l       开源,他是一款开源的免费软 ...

  9. nexus 私服相关的配置

    1 上传到nexus的配置 1 settings.xml <server> <id>releases</id> <username>admin</ ...

  10. 安卓多个RecyclerView滑动与显示问题

    最近在项目遇到这样的问题:在一线性垂直布局内,有两个垂直的RecyclerView,如果直接高度直接设置wrap-content, 通常会导致滑动冲突或是内容显示不全. 首先说下解决的思路,就是在最外 ...