16 python xml模块
1、基本概念
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单。
不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀。
至今很多传统公司如金融行业的很多系统的接口还主要是xml。
xml的格式如下,就是通过<>节点来区别数据结构的:
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year attr_test="yes">2009</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year attr_test="yes">2012</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year attr_test="yes">2012</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
<state>
<name>德州</name>
<population>德州</population>
</state>
</data>
xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml
2、遍历xml文档
import xml.etree.ElementTree as ET
tree = ET.parse("xmltest.xml")
root = tree.getroot()#相当于f.seek(0)
print(root.tag)#输出最开始的date
#遍历xml文档
for child in root:
print(child.tag, child.attrib
for i in child:
print(i.tag,i.text)
#------------------------------------------------------
---------- country {'name': 'Liechtenstein'}#child.tag,child.attrib
rank 2
year 2009
gdppc 141100#i.tag,i.text
neighbor None
neighbor None
---------- country {'name': 'Singapore'}
rank 5
year 2012
gdppc 59900
neighbor None
---------- country {'name': 'Panama'}
rank 69
year 2012
gdppc 13600
neighbor None
neighbor None
---------- state {}
name 德州
population 德州
#只遍历year 节点
for node in root.iter('year'):
print(node.tag,node.text)
#-----------------------------------
year 2009
year 2012
year 2012
3、修改和删除xml文档内容
import xml.etree.ElementTree as ET
tree = ET.parse("xmltest.xml")
root = tree.getroot()
#修改
for node in root.iter('year'):
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("updated","yes")
tree.write("xmltest.xml")
#删除node
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('output.xml')
4、自己创建xml文档
import xml.etree.ElementTree as ET
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = ''
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '
et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True)
ET.dump(new_xml) #打印生成的格式

16 python xml模块的更多相关文章
- Python xml 模块
Python xml 模块 TOC 什么是xml? xml和json的区别 xml现今的应用 xml的解析方式 xml.etree.ElementTree SAX(xml.parsers.expat) ...
- Python xml模块
xml模块 自己创建xml文档 import xml.etree.cElementTree as ET new_xml = ET.Element("personinfolist") ...
- Day 16 : Python 时间模块[time,]datetime[]及第三方模块的下载与安装
在进行python程序开发时,除了可以使用python内置的标准模块外,还右许多第三方模块使用,可以在python官网找到. 在使用第三方模块时,需要下载并安装此模块,然后就可以使用标准模块一样导入并 ...
- python(32)——【shelve模块】【xml模块】
一. shelve模块 json和pickle模块的序列化和反序列化处理,他们有一个不足是在python 3中不能多次dump和load,shelve模块则可以规避这个问题. shelve模块是一个简 ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)
logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...
- python常用模块:pickle、shelve、json、xml、configparser
今日内容主要有: 一.pickle模块二.shelve模块三.json模块四.json练习五.xml模块 六.xml练习七.configparser模块 一.pickle模块 #pickle是一个用来 ...
- python解析xml模块封装代码
在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...
- python全栈开发-hashlib模块(数据加密)、suprocess模块、xml模块
一.hashlib模块 1.什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 ...
随机推荐
- Java Mail多人群发与多附件发送
近期公司的项目用到了Java Mail来发送注冊邮件,只是.开发的时候都是使用封装好的JAR,曾经也不是非常了解Java Mail的使用原理. 网上非常多代码都是仅仅有一部分,看一看也跑不起来 ...
- VBox修改uuid
1.使用VBoxManage命令时,需要先在命令行中切换到VirtualBox的安装目录下 2.修改vdi的uuid:VBoxManage internalcommands sethduuid D: ...
- VBA文本型数字变成数值
sub test()with activesheet .usedrange.numberformatlocal="" .usedrange=.usedrange.valueen ...
- sping mvc+uploadify 上传文件大小控制3部曲
页面使用uploadify 上传控件,使用spring CommonsMultipartipartResolver , 反向代理nginx nginx 配置文件 client_max_body_siz ...
- 1110 Complete Binary Tree (25 分)
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
- select count(*) as total from(select count(*) from tab_cb_casim group by `card_no`) as cai;
子查询必须加一个别名才能执行!!
- Javascript框架
网易开源框架http://www.oschina.net/p/nej http://www.linuxeden.com/html/develop/20120716/127404.html 16 款最流 ...
- javascript创建对象之构造函数模式(二)
对上一章节的工厂模式进行代码重写 function Human(name, sex) { this.name = name; this.sex = sex; this.say = function ( ...
- Bind2nd源码解析
例:transform(coll1.begin(), coll1.end(), back_inserter(coll2), bind2nd(multiplies<int>(), 10)); ...
- Salesforce开源TransmogrifAI:用于结构化数据的端到端AutoML库
AutoML 即通过自动化的机器学习实现人工智能模型的快速构建,它可以简化机器学习流程,方便更多人利用人工智能技术.近日,软件行业巨头 Salesforce 开源了其 AutoML 库 Transmo ...