Python学习---重点模块之xml
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单
数据准备
<?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
import xml.etree.ElementTree as ET tree = ET.parse("hhh.xml")
root = tree.getroot()
print(root.tag) # 遍历xml文档
for child in root:
print(child.tag, child.attrib)
for i in child:
print(i.tag, i.text) # 只遍历year 节点
for node in root.iter('year'):
print(node.tag, node.text)
# ---------------------------------------
修改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")
删除xml
# 删除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={"enrolled": "yes"})
age = ET.SubElement(name, "age", attrib={"checked": "no"})
sex = ET.SubElement(name, "sex")
sex.text = '33'
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '19' et = ET.ElementTree(new_xml) # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True) ET.dump(new_xml) # 打印生成的格式
Python学习---重点模块之xml的更多相关文章
- Python学习---重点模块的学习【all】
time [时间模块] import time # print(help(time)) # time模块的帮助 print(time.time()) # 时间戳 print(time.cloc ...
- Python学习---重点模块之re
正则表达式是用来操作字符串,但是字符串提供的正则是完全匹配,有时候我们需要进行模糊匹配,这个时候就需要正则表达式了.通过re模块来实现,由C语言来执行底层的匹配 字符匹配(普通字符,元字符): 1 普 ...
- Python学习---重点模块之subprocess
subprocess是用来执行系统程序,查看系统的模块, 查看当前目录 第一种方法: import subprocess # subprocess会单独自己开辟一个线程,内部是多线程 # stdout ...
- Python学习---重点模块之logging
日志级别 日志级别 critical > error > warning > info > debug, 默认是从warning开始打印 import logging # 日 ...
- Python学习---重点模块之configparse
configparse模块常用于生成和修改常见的配置文档 生成配置模块:用字典写 import configparser config = configparser.ConfigParser() co ...
- Python学习---重点模块之json
注意:JSON不能转换类,不能转换函数 重点方法示例 json.dumps():实现文件写入,字符串转换[写入文件当然是JSON字符串楼] 实际上,json.dumps()只是帮我们做了一个字符串的转 ...
- Python学习---重点模块之pickle
仅仅支持Python里面的函数等相关功能的实现,而且pickle写入的内容是看不出来的,读取的时候要求有原内容 pickled的写入: import pickle def fun(): print(' ...
- Python学习---重点模块之shelve
简单示例 import shelve f = shelve.open(r'shelve.txt') f['info'] = {'name':'ftl', 'age':23, 'sex': 'male' ...
- Python学习--Selenium模块
1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台
随机推荐
- (转)虚拟路由器冗余协议【原理篇】VRRP详解
原文:http://blog.51cto.com/zhaoyuqiang/1166840 为什么要使用VRRP技术 我们知道,为了实现不同子网之间的设备通信,需要配置路由.目前常用的指定路由方法有两种 ...
- C#利用WebBrowser获取完整COOKIE
代码: http://www.cnblogs.com/hsapphire/archive/2010/09/10/1823384.html http://blog.csdn.net/attilax/ar ...
- Jmeter性能测试之添加思考时间
利用定时器添加用户思考时间 JMeter如何插入思考时间,在一个真实的性能测试场景中,是需要加入思考时间,来模拟真实用户行为.本文就来介绍,如何在三个请求之间添加思考时间. 1. 在Test Plan ...
- JDBC(3)-使用PreparedStatement接口实现增、删、改操作
1.PreparedStatement接口引入 PreparedStatement是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement ...
- Emacs as a Python IDE(转)
赋闲脱产的半年里,自己用C++/Java/Lisp胡乱写了几万行的代码,到了现在的公司,给OpenStack项目贴牛皮藓,反倒是Python用得最多.作为公司里面唯一的Emacser(没准也是 公司里 ...
- oracle connect by用法
先用scott用户下的emp表做实验.emp表有个字段,一个是empno(员工编号),另一个是mgr(上级经理编号)下面是表中所有数据 1 select * from emp start with e ...
- 九度oj 1031 xxx定律 2009年浙江大学计算机及软件工程研究生机试真题
题目1031:xxx定律 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5153 解决:3298 题目描述: 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n ...
- wtl学习总结
在windows平台,相比MFC,我更喜欢WTL,因其简洁漂亮.所以陆续花了一年的时间学习之,这里总结一下(在学习Wtl/Atl之前,最好是对WinApi编程有一定的了解). 安装 Wtl主页 htt ...
- 进入与退出anconda虚拟环境
# 进入虚拟环境,使用 # # $ conda activate spider-venv # # 退出虚拟环境,使用 # # $ conda deactivate
- WES7 定制界面完整过程(去除所有windows标识)
转载但有改动 红色字体记录 目的:实验从启动开始到出现桌面,不出现任何windows图标或标识.重大提示:在某些虚拟机上面操作和真实机器是不一样的,主机会容易很多;所以在虚拟机无法实现效果的时候使用主 ...