xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用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("xml_test.xml") #open
root = tree.getroot() #f.seek(0)
print(root.tag) #遍历xml文档
for child in root: #data 下一层的关键词country
print(child.tag, child.attrib)
print('----i start----')
for i in child:
print(i.tag,i.text)
print('----i stop----') #只遍历year 节点
print('----year----')
for node in root.iter('year'):
print(node.tag,node.text)

运行结果

data
country {'name': 'Liechtenstein'}
----i start----
rank 2
year 2008
gdppc 141100
neighbor None
neighbor None
----i stop----
country {'name': 'Singapore'}
----i start----
rank 5
year 2011
gdppc 59900
neighbor None
----i stop----
country {'name': 'Panama'}
----i start----
rank 69
year 2011
gdppc 13600
neighbor None
neighbor None
----i stop----
----year----
year 2008
year 2011
year 2011

修改和删除xml文档

import xml.etree.ElementTree as ET

tree = ET.parse("xml_test.xml") #open
root = tree.getroot() #f.seek(0) #修改
for node in root.iter('year'): #只从下一层里面找
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("attr_test","yes") #设置属性 tree.write("xmltest.xml") #删除node
for country in root.findall('country'): #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

root = ET.Element("namelist") #root

name = ET.SubElement(root,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = '' name2 = ET.SubElement(root,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '' et = ET.ElementTree(root) #生成文档对象
et.write("build_out.xml", encoding="utf-8", xml_declaration=True) ET.dump(root) #打印生成的格式

Python全栈之路----常用模块----xml处理模块的更多相关文章

  1. Python全栈之路----常用模块----hashlib加密模块

    加密算法介绍 HASH       Python全栈之路----hash函数 Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列 ...

  2. Python全栈之路----常用模块学习----模块的种类和导入方法

    什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...

  3. Python全栈之路----常用模块----datetime模块详解

    相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...

  4. Python全栈之路----常用模块----shutil模块

    高级的 文件.文件包.压缩包 处理模块   参考Python之路[第四篇]:模块     #src是原文件名,fdst是新文件名 shutil.copyfileobj(fsrc, fdst[, len ...

  5. Python全栈之路----常用模块----软件开发目录规范

    目录基本内容 log  #日志目录 conf  #配置目录 core/luffycity  #程序核心代码目录  #luffycity 是项目名,建议用小写 libs/modules  #内置模块 d ...

  6. Python全栈之路----常用模块----re 模块

    正则表达式就是字符串的匹配规则,在多数编程语言里都有相应的支持,python里对应的模块是 re. re的匹配语法有以下几种 re.match 从头开始匹配 re.search 匹配包含 re.fin ...

  7. Python全栈之路----常用模块----logging模块

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...

  8. Python全栈之路----常用模块----subprocess模块

    我们经常需要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就是发起一个新进程,通过python调用系统命令或脚本的模块在python ...

  9. Python全栈之路----常用模块----序列化(json&pickle&shelve)模块详解

    把内存数据转成字符,叫序列化:把字符转成内存数据类型,叫反序列化. Json模块 Json模块提供了四个功能:序列化:dumps.dump:反序列化:loads.load. import json d ...

随机推荐

  1. python修炼第四天

    今天换了师傅.江湖人称景女神^o^. 女师傅讲的比较细,原理的比较多.初学者来说有些难.但是基本功是必须要打牢的.努力! 迭代器 迭代器,迭代的工具1 什么是迭代,指的是一个重复的过程,每一次重复称为 ...

  2. Spark 中Java实现数据库Row转Rating

    Dataset<Row> ratings = mlsc.sql("SELECT user,movie,rating FROM data");JavaRDD<Row ...

  3. shell 下生成使用UUID

    #!/bin/bash psd="/proc/sys/kernel/random/uuid" echo $(cat $psd)UUID=$(cat /proc/sys/kernel ...

  4. Ubuntu下安装chrome浏览器

    1.在终端中,输入以下命令: sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/sources.l ...

  5. 响应式编程系列(一):什么是响应式编程?reactor入门

    响应式编程 系列文章目录 (一)什么是响应式编程?reactor入门 (二)Flux入门学习:流的概念,特性和基本操作 (三)Flux深入学习:流的高级特性和进阶用法 (四)reactor-core响 ...

  6. using Newtonsoft.Json;

    using Newtonsoft.Json;    //数组转义为json  string result = JsonConvert.SerializeObject(list1);   //josn转 ...

  7. DAY2练习-购物车

    print('欢迎访问购物车')money = int(input('为方便购物,请输入您的总资产:')) #输入金钱必须为数字类型shopping_price_list = [{"name ...

  8. 本周HTML5的知识点

    html5一般用<meta>标签描述网页的摘要信息.标题标签一共有6个,标题字体加粗<h1>最大,<h6>最小. <p>标签标示内容都在一行显示,结束后 ...

  9. Excel身份证验证,身份证校验公式

    =IF(LEN(Q4)=0,"空",IF(LEN(Q4)=15,"老号",IF(LEN(Q4)<>18,"位数不对",IF(CH ...

  10. PHP安装过程中问题详解

    安装Apace时我就犯了一个大错误.因为我的母语是JAVA,我以为Tomcat就是Apache.其实不然,Tomcat是给Java用的,处理JSP等的动态页面. 而PHP则是单纯的用Apache安装A ...