20180209-xml模块
xml的用法操作如下:
xml格式如下:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
xml协议在各个语言中都是支持的,在python中可以使用如下模块操作xml
import xml.etree.ElementTree as ET
tree = ET.parse('xmltest.xml')
root = tree.getroot()
print(root.tag)
# 输出
# data
# 遍历xml文档
for child in root:
print(child.tag,child.attrib)
# 输出
# country {'name': 'Liechtenstein'}
# country {'name': 'Singapore'}
# country {'name': 'Panama'}
for node in root:
for child in node:
print(child.tag,child.text)
# 输出
# rank 2
# year 2008
# gdppc 141100
# neighbor None
# neighbor None
# rank 5
# year 2011
# gdppc 59900
# neighbor None
# rank 69
# year 2011
# gdppc 13600
# neighbor None
# neighbor None
# 只遍历year节点
for node in root.iter('year'):
print(node.tag,node.text)
# 输出
# year 2008
# year 2011
# year 2011
修改和删除xml文档
# 修改
for node in root.iter('year'):
new_year = int(node.text) + 1
node.text = str(new_year)
node.set('update','yes')
tree.write('xmltest.xml') # 只遍历year节点
for node in root.iter('year'):
print(node.tag,node.text)
# 输出
# year 2009
# year 2012
# year 2012
# 删除node
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country) tree.write('output.xml')
自己创建xml文档
import xml.etree.ElementTree as ET
new_xml = ET.Element('namelist')
name = ET.SubElement(new_xml,'name',attrib={'enrolld': 'yes'})
age = ET.SubElement(name,'age',attrib={'checed': 'no'})
sex = ET.SubElement(name,'sex')
sex.text = '男'
name2 = ET.SubElement(new_xml,'name',attrib={'enrolld': '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) #打印生成格式
20180209-xml模块的更多相关文章
- oop、configparser、xml模块
本节大纲:一:在python中,有两种编程思想.1:函数式编程.2:oop.无论是函数式编程还是oop都是相辅相成.并不是说oop比函数式编程就好.各有各的优缺点.在其他语言中java等只能以面向对象 ...
- python解析xml模块封装代码
在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- python全栈开发-hashlib模块(数据加密)、suprocess模块、xml模块
一.hashlib模块 1.什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 ...
- 【python标准库模块五】Xml模块学习
Xml模块 xml本身是一种格式规范,是一种包含了数据以及数据说明的文本格式规范.在json没有兴起之前各行各业进行数据交换的时候用的就是这个.目前在金融行业也在广泛在运用. 举个简单的例子,xml是 ...
- Python xml 模块
Python xml 模块 TOC 什么是xml? xml和json的区别 xml现今的应用 xml的解析方式 xml.etree.ElementTree SAX(xml.parsers.expat) ...
- python之xml模块
# XML 模块的操作参考链接 # http://www.cnblogs.com/yuanchenqi/articles/5732581.html
- hashlib,hmac,subprocess,configparser,xlrd,xlwt,xml模块基本功能
hashlib模块:加密 import hashlib# 基本使用cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8'))print(cipher.h ...
- python configparse模块&xml模块
configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...
- xml模块、项目开发过程
一.XML模块 xml指的是可扩展标记语言,是一种定义电子文档结构和描述的语言,可以用来标记数据.定义数据类型. 什么时候用xml? 当需要自定义文档结构时,使用xml.在java中经常会使用xml来 ...
随机推荐
- Redis安装配置以及开机启动
1.下载源码,解压缩后编译源码. $ wget http://download.redis.io/releases/redis-2.8.3.tar.gz $ .tar.gz $ cd redis- ...
- OSI参考模型与TCP/IP参考模型与TCP/IP协议栈
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484126.html OSI参考模型与TCP/IP参考模型与TCP/IP协议栈 TCP/IP分层模型 ...
- 026:if标签使用详解
if标签使用详解: if 标签: if 标签相当于 Python 中的 if 语句,有 elif 和 else 相对应,但是所有的标签都需要用标签符号 {% %} 进行包裹. if 标签中可以使 ...
- VSCode必备插件
通用组件 1.汉化插件 https://marketplace.visualstudio.com/items?itemName=MS-CEINTL.vscode-language-pack-zh-ha ...
- 如何查看MySQL数据库的版本
如何查看MySQL数据库的版本 一.总结 一句话总结: SQL语句:select version(); 命令行:mysql -V 或 mysql --version 二.三种方法查看MySQL数据库的 ...
- 2017工业软件top100
- EZOJ #362历史
分析 就是保存前pi-1个数每个ai出现多少次 然后维护这些数当前剩余的最大值 每次和新加进来的比较即可 如果新的大直接取 否则新的最大值一定不大于原来的最大值 因此o(n) 代码 #include& ...
- Hugo - 安装、设置及使用
Hugo 官方主页:https://gohugo.io 待选主题: https://github.com/cdipaolo/gindoro https://github.com/oserz/hugo- ...
- MongoDB分片配置 优化 不错
简单注解:mongos 路由进程, 应用程序接入mongos再查询到具体分片,监听端口默认27017config server 路由表服务, 每一台都具有全部chunk的路由信息 shard为数据存储 ...
- php Function ereg() is deprecated的解决方法
PHP 5.3 ereg() 无法正常使用,提示“Function ereg() is deprecated Error”.问题根源是php中有两种正则表示方法,一个是posix,一个是perl,ph ...