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模块)的更多相关文章

  1. python解析xml模块封装代码

    在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...

  2. Python之路(第十六篇)xml模块、datetime模块

    一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...

  3. 人生苦短之我用Python篇(深浅拷贝、常用模块、内置函数)

    深浅拷贝 有时候,尤其是当你在处理可变对象时,你可能想要复制一个对象,然后对其做出一些改变而不希望影响原来的对象.这就是Python的copy所发挥作用的地方. 定义了当对你的类的实例调用copy.c ...

  4. python之xml模块

    # XML 模块的操作参考链接 # http://www.cnblogs.com/yuanchenqi/articles/5732581.html

  5. Python的xml模块

    先来一段xml代码 <?xml version="1.0"?> <data> <country name="Liechtenstein&qu ...

  6. 人生苦短之我用Python篇(线程/进程、threading模块:全局解释器锁gil/信号量/Event、)

    线程: 有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.是一串指令的集合.线程是程序中一个单一的顺序控制流程.进程内一个相对独立的.可调度的执行单元,是 ...

  7. 人生苦短之我用Python篇(paramiko模块)

    该模块机遇SSH用于连接远程服务器并执行相关操作 基于用户名密码连接: import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在kno ...

  8. python的xml模块用法

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的 ...

  9. 人生苦短之我用Python篇(列表list、字典dict、元组tuple、字符串str)

    列表 创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_s ...

随机推荐

  1. vSphere SDK for Java - 从模板部署虚拟机并配置IP地址

    vSphere for Java类库:vijava    虚拟机配置类 package com.vmware.vcenter_event.VirtualMachine; import com.vmwa ...

  2. JVM反调调用优化,导致发生大量异常时log4j2线程阻塞

    背景 在使用log4j2打日志时,当发生大量异常时,造成大量线程block问题的问题. 一个关于log4j2的高并发问题:https://blog.fliaping.com/a-high-concur ...

  3. 如何使用openwrt下的分区表生成器ptgen

    1.基本用法如下: ptgen [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <ali ...

  4. mybatis缓存有关的设置和属性

    知识点:mybatis缓存相关的设置和属性 重点:每次执行增删改操作后,一二级缓存被清空,是因为标签设置默认属性为 flushCache="true" (1) <!-- 全局 ...

  5. 【Semantic segmentation】Fully Convolutional Networks for Semantic Segmentation 论文解析

    目录 0. 论文链接 1. 概述 2. Adapting classifiers for dense prediction 3. upsampling 3.1 Shift-and-stitch 3.2 ...

  6. response.getWriter().write("中文");乱码问题

    起初遇到这个问题,网上几乎所有的建议都是: response.setHeader("Content-type", "text/html;charset=UTF-8&quo ...

  7. RestTemplate请求https忽略证书认证

    RestTemplate是Spring提供的用于访问Rest服务的客户端,提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率.RestTemplate 默认使用J2SE提供的方式( ...

  8. 用svg实现不规则形状

    像这种弧形,用纯html和css很难写,但是用svg就简单多了. 可以用作图工具画出一个弧形,然后导成svg格式.在页面中,下面的白块就是div+svg构成 mixin svgCard(...cont ...

  9. FlashFXP客户端 FTP连接,连接很慢的情况,

    菜单栏-->站点-->站点管理器--->左边视图FTP--->列表命令选择 STAT -L

  10. JavaScript内部原理系列-变量对象(Variable object)

    概要 我们总是会在程序中定义一些函数和变量,之后会使用这些函数和变量来构建我们的系统.然而,对于解释器来说,它又是如何以及从哪里找到这些数据的(函数,变量)?当引用一个对象的时候,在解释器内部又发生了 ...