1.新建xml 
import xml.etree.ElementTree as ET
a=ET.Element('elem')
c=ET.SubElement(a,'child1')
c.text="some text"
d=ET.SubElement(a,'child2')
d.text="hellp"
root=ET.Element('root')
root.extend(a)
tree= ET.ElementTree(root)
tree.write("test3.xml")
注意:1.如果SubElement中没有内容,这个标签将是非闭合的。
2.一定要有extend()函数,用于列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
这里就是在root后面加标签 2.从xml中读数据
import xml.etree.ElementTree as ET
tree=ET.parse("test3.xml")
root=tree.getroot() for child in root:
print(child.tag) for child in root.findall("student"):
number=child.get('no')
name=child.find("name").text
print(number,name) 3.修改数据
for age in root.iter('age'):
  new_age = int(age.text) + 1
age.text = str(new_age)
age.set('updated', 'yes')
tree.write('test3.xml')


python3.x中xml.etree.ElementTree解析xml举例的更多相关文章

  1. python xml.etree.ElementTree解析xml文件获取节点

    <?xml version = "1.0" encoding = "utf-8"?> <root> <body name=&quo ...

  2. Python中xml.etree.ElementTree读写xml文件实例

    import osimport xml.etree.ElementTree as ET'''Python 标准库中,提供了6种可以用于处理XML的包,本文举实例说明第6种1.xml.dom2.xml. ...

  3. python xml.etree ElementTree解析 编辑 xml

    python有很多种xml解析方式,不过感觉etree的ElementTree 用起来最方便. #coding=utf-8 from xml.etree import ElementTree impo ...

  4. Python中使用ElementTree解析xml

    在Python中,ElementTree是我们常用的一个解析XML的模块 1.导入ElementTree模块 from xml.etree import ElementTree as ET 2.初始化 ...

  5. python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用

    1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...

  6. python 解析xml遇到xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 4, column 34

    在调试数字驱动用xml文件的方式时,包含读取xml文件的步骤,运行程序报错: d:\test\0629>python XmlUtil.pyTraceback (most recent call ...

  7. python 使用ElementTree解析xml

    以country.xml为例,内容如下: <?xml version="1.0"?> <data> <country name="Liech ...

  8. python标准库xml.etree.ElementTree的bug

    使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...

  9. [python]使用ElementTree解析XML【译】

    19.7 The ElementTree XML API 源码:Lib/xml/etree/ElementTree.py Element类型是一个灵活的容器对象,设计出来是用于存储有层次的数据结构到内 ...

随机推荐

  1. 【14】vuex2.0 之 mutation 和 action

    我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...

  2. Why Namespace?

    上一节我们讨论了 Neutron 将虚拟 router 放置到 namespace 中实现了不同 subnet 之间的路由.今天探讨为什么要用 namespace 封装 router? 回顾一下前面的 ...

  3. 洛谷 P1262 间谍网络==Codevs 4093 EZ的间谍网络

    4093 EZ的间谍网络 时间限制: 10 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B ...

  4. .NET抓取数据范例 抓取页面上所有的链接

    原文发布时间为:2009-11-15 -- 来源于本人的百度文章 [由搬家工具导入] .NET抓取数据范例 抓取页面上所有的链接 前台: <%@ Page Language="C#&q ...

  5. Windows开发

    1. 介绍 这里简单介绍了Windows应用程序开发的基础知识 2. 基础 Windows下的应用程序有控制台程序和Win32窗口程序,这里讲的是Win32窗口程序 Windows提供了相关静态库(L ...

  6. php--获取用户ip

    一般在做登录的时候有的会要求同一个帐号不能同时用不同的ip登录,这个时候我们需要获取到用户IP地址 获取ip地址的函数: function getIP() { if (getenv('HTTP_CLI ...

  7. Google的新一代一致性哈希算法

    先看到了一篇中文文章:谷歌退出有界负载的一致性哈希算法 然后找到了它对应的英文原文 以及它的英文paper,24页 又找到一篇博客,发现已经有人将它在haproxy上实现了,haproxy1.7.5上 ...

  8. Linux每日一坑002

    0.删除软连接目录时,目录后面一定不要有斜杠!最好用mv代替rm. 1.数据库安装后要初始化数据库,不然无法登陆,会报权限错误,原谅我的无知,跪了. mysql_install_db --user=m ...

  9. Linux系统日常运维-修改IP地址

    分享下高手写的很好的文章 IP地址.子网掩码.网络号.主机号.网络地址.主机地址 step 0: check the iptables.selinux service iptables iptable ...

  10. 身份识别协议枚举工具ident-user-enum

    身份识别协议枚举工具ident-user-enum   身份识别协议(Ident protocol,IDENT)是一种Internet协议,用于识别使用特定TCP端口的用户身份.服务器开启该服务后,会 ...