人生苦短之我用Python篇(XML模块)
XML模块
http://baike.baidu.com/link?url=-mBgvMdEDU7F05Pw7h_hBt7A0ctYiPm5a_WvKVLknydnRXKRIyydcVZWRjd_5HE2fMW5Ic45Nb4DiuH1azLjjfs1HBMER-4j38pqFgmj4z4spRud6DrmYimbttjfSB105KHP2pDzaVKmfFvL1JrZ4cpmHqEiFiuSZ_LehN3Hp5GqLdEb6mohzYRyFIZadILH3iDqyvAjCbEXGr4qLMLZEq
可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。
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>
xmltest.xml
tree = ET.parse("xmltest.xml")#解析xml文件
root = tree.getroot()#获取根节点
node.tag
node.attrib
node.text
node.tag = "years" #修改tag的值
node.text = str(new_year) #赋予node元素新的值
node.set("updated","no") #设置node元素的属性值
root.remove(country)
在python中可以用以下模块操作xml(import xml.etree.ElementTree as ET)
import xml.etree.ElementTree as ET
tree = ET.parse("xmltest.xml")#解析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.attrib,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")#解析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.attrib,i.text)
#
# # 只遍历year 节点
# for node in root.iter('year'):
# print(node.tag, node.text) 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元素新的值
node.set("updated","no")#设置node元素的属性值 tree.write("xmltest.xml") #删除
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")
name.text = '陈泰成'
age.text = ""
sex.text = '男' name = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name,"age")
name.text = "哈利路亚"
age.set("is_privary","yes")
age.text = "" et = ET.ElementTree(new_xml)
et.write("test.xml",encoding = "utf-8",xml_declaration = True)
ET.dump(new_xml)
人生苦短之我用Python篇(XML模块)的更多相关文章
- python解析xml模块封装代码
在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...
- Python之路(第十六篇)xml模块、datetime模块
一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...
- 人生苦短之我用Python篇(深浅拷贝、常用模块、内置函数)
深浅拷贝 有时候,尤其是当你在处理可变对象时,你可能想要复制一个对象,然后对其做出一些改变而不希望影响原来的对象.这就是Python的copy所发挥作用的地方. 定义了当对你的类的实例调用copy.c ...
- python之xml模块
# XML 模块的操作参考链接 # http://www.cnblogs.com/yuanchenqi/articles/5732581.html
- Python的xml模块
先来一段xml代码 <?xml version="1.0"?> <data> <country name="Liechtenstein&qu ...
- 人生苦短之我用Python篇(线程/进程、threading模块:全局解释器锁gil/信号量/Event、)
线程: 有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.是一串指令的集合.线程是程序中一个单一的顺序控制流程.进程内一个相对独立的.可调度的执行单元,是 ...
- 人生苦短之我用Python篇(paramiko模块)
该模块机遇SSH用于连接远程服务器并执行相关操作 基于用户名密码连接: import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在kno ...
- python的xml模块用法
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的 ...
- 人生苦短之我用Python篇(列表list、字典dict、元组tuple、字符串str)
列表 创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_s ...
随机推荐
- 用python打印99乘法口诀表
代码如下 #!/usr/bin/env python # encoding: utf-8 __author__ = 'Nicholas.Cage' i = 1 j = 1 while i <= ...
- 【转载】showModalDialog returnValue is undefined in Google Chrome
showModalDialog returnValue is undefined in Google Chrome Posted on August 22, 2012by briancaos For ...
- Java GC垃圾回收
Java的内存分配和回收也主要在Java的堆上进行的,Java的堆中存储了大量的对象实例,所以Java的堆也叫GC堆. Java在垃圾收集的过程中,主要用到了分代收集算法,具体有复制.标记清除.标记压 ...
- VirtualBox安装RedHat7
软件准备 VirtualBox-5.2.8-121009-Win.exe rhel-server-7.4-x86_64-dvd.iso 安装环境 win10 安装步骤: 1.先在win10系统中安装V ...
- spark(二)优化思路
优化思路 内存优化 内存优化大概分为三个方向 1.所有对象的总内存(包括数据和java对象) 2.访问这些对象的开销 3.垃圾回收的开销 其中Java的原生对象往往都能被很快的访问,但是会多占据2-5 ...
- C#错误的面试题
你面试的时候是不是碰到这样的问题 String s = new String("xyz");创建了几个String Object? ,对于这个问题,我之前也一直以为是对的,这样写是 ...
- Gray Code,求格林码
问题描述: The gray code is a binary numeral system where two successive values differ in only one bit. G ...
- 一切从Trade开始(转)
taobao.trades.sold.get 取得交易列表 从店铺取得时间 taobao.shop.get (nick,field)shop.created 循环读取从开店时间到当前时间的所有交易 t ...
- Oracle数据库报错: ORA-29275:部分多字节字符
Oracle数据库报错: ORA-29275:部分多字节字符 就是你查出来的字符串(有汉字的)有可能会出问题, 在认为有问题的字段 用 to_nchar("字段")包起来 比如 你 ...
- 数组中的k个最小值
问题:输入n个整数,找出其中最小的k个数. 方案一:将输入的n个整数进行排序,输出前k个数即为所求的k个最小数.时间复杂度为O(nlogn). 方案二:创建一个大小为k的容器,来存储最小的k个数.遍历 ...