---恢复内容开始---

xml 通过< >节点来区别数据结构

---恢复内容结束---

xml 通过< >节点来区别数据结构

<xml version='1.0'>
<data>
<country name="USA">
<rank updated="yes">19</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name ="Austria" direction="E"/>
</country>
<country name="Singapore">
<rank updated="yes">99</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name ="Switzerland" direction = "W"/>
</country>
</data>
 # !/user/bin/python
# -*- coding: utf-8 -*- import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml")
root = tree.getroot() # xml的内存地址
print(root.tag) # xml中的标签名 # 遍历xml文件
for child in root:
print(child.tag, child.attrib)
for i in child:
print(i.tag, i.text, i.attrib) # 只遍历year节点
for node in root.iter('year'):
print(node.tag, node.text) # 修改xml文档的内容
for node in root.iter('year'):
new_year = int(node.text)+1
node.text = str(new_year)
node.set("updated", "yes") # 给year 添加属性updated, 值为yes
node.set("updated by", "Alex") # 给year 添加属性updated by, 值为Alex
tree.write("xmltest.xml") # 删除node
for country in root.findall("country"):
rank = int(country.fine('rank').text)
if rank > 30: # rank的值大于30时,删掉相应的国家
root.remove('country')
tree.write("output.xml")
# !/user/bin/python
# -*- coding: utf-8 -*- import xml.etree.ElementTree as ET new_xml = ET.Element("personinfolist")
personinfo = ET.SubElement(new_xml, "personinfo", attrib={"enrolled":"yes"})
name = ET.SubElement(personinfo, "name")
name.text = "Alex"
age = ET.SubElement(personinfo, "age", attrib={"checked":"no"})
sex = ET.SubElement(personinfo, "sex")
age.text = ""
personinfo2 = ET.SubElement(new_xml, "personinfo", attrib={"enrolled":"no"})
name = ET.SubElement(personinfo2, "name")
name.text = "Lucy"
age = ET.SubElement(personinfo2, "age")
age.text = "" et = ET.ElementTree(new_xml) # 生成文件对象
et.write("text.xml", encoding='utf-8',xml_declaration=True) ET.dump(new_xml) # 打印生成的格式

Python xml处理模块的更多相关文章

  1. python xml.dom模块解析xml

    1. 什么是xml?有何特征? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 例子:del.xml <?xml version=&q ...

  2. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

  3. Python xml 模块

    Python xml 模块 TOC 什么是xml? xml和json的区别 xml现今的应用 xml的解析方式 xml.etree.ElementTree SAX(xml.parsers.expat) ...

  4. python的XML处理模块ElementTree

    ElementTree是python的XML处理模块,它提供了一个轻量级的对象模型.它在Python2.5以后成为Python标准库的一部分,但是Python2.4之前需要单独安装.在使用Elemen ...

  5. Day6 - Python基础6 模块shelve、xml、re、subprocess、pymysql

    本节目录: 1.shelve模块 2.xml模块 3.re模块 4.subprocess模块 5.logging模块 6.pymysql 1.shelve 模块 shelve模块是一个简单的k,v将内 ...

  6. python基础-7模块,第三方模块安装方法,使用方法。sys.path os sys time datetime hashlib pickle json requests xml

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  7. 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)

    一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...

  8. Python自动化 【第五篇】:Python基础-常用模块

    目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...

  9. 【Python之路】第六篇--Python基础之模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

随机推荐

  1. 面试之路(6)-BAT面试之操作系统内存详解

    本文主要参考两篇博客,读后整理出来,以供大家阅读,链接如下: http://blog.jobbole.com/95499/?hmsr=toutiao.io&utm_medium=toutiao ...

  2. leetcode之旅(7)-Move Zeroes

    Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...

  3. 去除元素浮动(:after)

    >>HTML <div class="zg_city"> <div class="zg_left"></div> ...

  4. balanced binary tree(判断是否是平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. 线上Django项目python2到3升级日记

    这两天干了一个几斤疯狂的事情,花不到一个工作日的时间把一个线上Django项目语言版本从python2升级到Python31.字典的一个语法变化 Python2.7: if dict1.haskey( ...

  6. Ocelot中文文档-授权

    Ocelot支持基于声明的授权. 这意味着如果您有要授权的路由,您可以将以下内容添加到您的ReRoute配置中. "RouteClaimsRequirement": { " ...

  7. jdk和tomcat环境配置

    一.安装JDK和Tomcat 1,安装JDK:直接运行jdk-7-windows-i586.exe可执行程序,默认安装即可. 备注:路径可以其他盘符,不建议路径包含中文名及特殊符号. 2.安装Tomc ...

  8. 解析xml字符串时报“前言中不允许有内容”错误。

    一,问题出现经过: j基于java语言webservic服务端接收客户端 传来的xml字符串用 解析时总报:org.dom4j.DocumentException: Error on line 1 o ...

  9. 用户注册登录系统 V2.0

    # 准备空列表 users = [] # 准备当前在线用户 online_user = {} while True: # 打印系统提示 print("欢迎使用 用户注册登录系统V2.0&qu ...

  10. cocos2dx中的坐标系统

    一. (1)OpenGL坐标系 Cocos2D-x以OpenGL和OpenGL ES为基础,所以自然支持OpenGL坐标系.该坐标系原点在屏幕左下角,x轴向右,y轴向上. (2)屏幕坐标系 屏幕坐标系 ...