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. echarts中关于merge的代码

    function merge(target, source, overwrite) { // We should escapse that source is string // and enter ...

  2. [LeetCode] Remove Duplicates from Sorted List 链表

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. EL与OGNL

    EL表达式:  >>单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application. >>如果在struts环境中,它除了有在上 ...

  4. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---35

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  5. Selenium2+python自动化(学习笔记2)

    from selenium import webdriverdriver = webdriver.Ie()driver.get=("http://www.baidu.com")dr ...

  6. 【C/C++】知识点

    1.C++中的参数传递机制:值传递.指针传递.引用传递 2.C++的内部类和外部类: 一个讲得不错的博客,不过不让转载:C++内部类 3.static 可以修饰局部变量.全局变量和函数. 不可修饰类! ...

  7. OSX 系统无法直接用 Chrome 双击点击打开本地 html 文件

    在 Mac OS X 下,文件经常会被附加上 OS X 特有的扩展属性 ( extend attributes ),具体表现是用 ls -l 查看时会有 @ 的标记,譬如: $ ls -l index ...

  8. 分享Kali Linux 2017年第17周镜像文件

     分享Kali Linux 2017年第17周镜像文件  Kali Linux官方于4月23日发布2017年的第17周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KD ...

  9. [TJOI2014] Alice and Bob

    非常好的一道思维性题目,想了很久才想出来qwq(我好笨啊) 考虑a[]数组有什么用,首先可以yy出一些性质 (设num[i]为原来第i个位置的数是什么 , 因为题目说至少有一个排列可以满足a[],所以 ...

  10. 某考试 T3 C

    找不着原题了. 原题大概就是给你一条直线上n个点需要被覆盖的最小次数和m条需要花费1的线段的左右端点和1条[1,n]的每次花费为t的大线段. 问最小花费使得所有点的覆盖数都达到最小覆盖数. 感觉这个函 ...