python数据交换模块-XML

简介:

XML 可扩展标记语言

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

xml的格式如下,就是通过<>节点来区别数据结构的:

<?xml version="1.0"?>
<data>
        <country personinfo="Liechtenstein">
                <rank updated="yes">2</rank>
                <year>2008</year>
                <gdppc>141100</gdppc>
                <neighbor personinfo="Austria" direction="E"/>
                <neighbor personinfo="Switzerland" direction="W"/>
        </country>
        <country personinfo="Singapore">
                <rank updated="yes">5</rank>
                <year>2011</year>
                <gdppc>59900</gdppc>
                <neighbor personinfo="Malaysia" direction="N"/>
        </country>
        <country personinfo="Panama">
                <rank updated="yes">69</rank>
                <year>2011</year>
                <gdppc>13600</gdppc>
                <neighbor personinfo="Costa Rica" direction="W"/>
                <neighbor personinfo="Colombia" direction="E"/>
        </country>
</data>

xml协议在各个语言里的都是支持的,在python中可以用以下模块操作xml  

import xml.etree.ElementTree as ET   #导入模块别名为ET
tree = ET.parse("xml-test.xml")    #打开xml-test.xml文件
root = tree.getroot()
print(root)             #是一个内存地址
print(root.tag)         #是xml文件的tag
#遍历xml文档
for child in root:
        print(child.tag,child.attrib,child.text)
        for i in child:
                print(i.tag,i.text,i.attrib)

#只遍历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'):      #只遍历iter
        new_year = int(node.text) + 1
        node.text = str(new_year)
        node.set("updated","yes")

tree.write("xmltest.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("personinfolist")        #根节点
personinfo = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yes"}) #new_xml子节点、节点名字、节点属性
name = ET.SubElement(personinfo,"name")
name.text = "aaa li"
age = ET.SubElement(personinfo,"age",attrib={"checked":"no"}) #
sex = ET.SubElement(personinfo,"sex")
age.text = '33'

personinfo2 = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"no"})
name2 = ET.SubElement(personinfo2,"name")
name2.text = "aaa wang"
age = ET.SubElement(personinfo2,"age")
age.text = '19'

et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True)

ET.dump(new_xml) #打印生成的格式
结果:
<?xml version='1.0' encoding='utf-8'?>
<personinfolist>
    <personinfo enrolled="yes">
        <name>aaa li</name>
        <age checked="no">33</age>
        <sex />
    </personinfo>
    <personinfo enrolled="no">
        <name>aaa wang</name>
        <age>19</age>
    </personinfo>

</personinfolist>




本文转自506554897 51CTO博客,原文链接:,如需转载请自行联系原作者 http://blog.51cto.com/506554897/2047335

相关文章
|
22天前
|
存储 开发者 Python
Python中的collections模块与UserDict:用户自定义字典详解
【4月更文挑战第2天】在Python中,`collections.UserDict`是用于创建自定义字典行为的基类,它提供了一个可扩展的接口。通过继承`UserDict`,可以轻松添加或修改字典功能,如在`__init__`和`__setitem__`等方法中插入自定义逻辑。使用`UserDict`有助于保持代码可读性和可维护性,而不是直接继承内置的`dict`。例如,可以创建一个`LoggingDict`类,在设置键值对时记录操作。这样,开发者可以根据具体需求定制字典行为,同时保持对字典内部管理的抽象。
|
23天前
|
存储 缓存 算法
Python中collections模块的deque双端队列:深入解析与应用
在Python的`collections`模块中,`deque`(双端队列)是一个线程安全、快速添加和删除元素的双端队列数据类型。它支持从队列的两端添加和弹出元素,提供了比列表更高的效率,特别是在处理大型数据集时。本文将详细解析`deque`的原理、使用方法以及它在各种场景中的应用。
|
4天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
5天前
|
测试技术 Python
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
|
5天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
38 1
|
7天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
47 0
|
9天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
10天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
15天前
|
索引 Python
「Python系列」Python operator模块、math模块
Python的`operator`模块提供了一系列内置的操作符函数,这些函数对应于Python语言中的内建操作符。使用`operator`模块可以使代码更加清晰和易读,同时也能提高性能,因为它通常比使用Python内建操作符更快。
27 0
|
20天前
|
数据采集 网络协议 API
python中其他网络相关的模块和库简介
【4月更文挑战第4天】Python网络编程有多个流行模块和库,如requests提供简洁的HTTP客户端API,支持多种HTTP方法和自动处理复杂功能;Scrapy是高效的网络爬虫框架,适用于数据挖掘和自动化测试;aiohttp基于asyncio的异步HTTP库,用于构建高性能Web应用;Twisted是事件驱动的网络引擎,支持多种协议和异步编程;Flask和Django分别是轻量级和全栈Web框架,方便构建不同规模的Web应用。这些工具使网络编程更简单和高效。