python之模块分类(二)
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单。在json诞生之前,计算机开发界基本都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要是xml。许多运维工具的配置以及KVM虚拟机的配置文件都是xml格式。
一下通过KVM虚拟机的配置文件,来介绍xml文件的风格,其就是通过<>节点来区别数据结构的:
<domain type="kvm"> <name>Centos7u4-1</name> <uuid>f9dfe67c-78c3-4f22-a6f1-7eea9288b62e</uuid> <memory unit="KiB">1048576</memory> <currentMemory unit="KiB">1048576</currentMemory> <vcpu placement="static">1</vcpu> <os> <type arch="x86_64" machine="pc-i440fx-rhel7.0.0">hvm</type> <boot dev="hd" /> </os> <features> <acpi /> <apic /> </features> <cpu check="partial" match="exact" mode="custom"> <model fallback="allow">IvyBridge</model> </cpu> <clock offset="utc"> <timer name="rtc" tickpolicy="catchup" /> <timer name="pit" tickpolicy="delay" /> <timer name="hpet" present="no" /> </clock> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <pm> <suspend-to-mem enabled="no" /> <suspend-to-disk enabled="no" /> </pm> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk device="disk" type="file"> <driver name="qemu" type="qcow2" /> <source file="/data/vmdisk/template-centos7u4.qcow2" /> <target bus="ide" dev="hda" /> <address bus="0" controller="0" target="0" type="drive" unit="0" /> </disk> <disk device="cdrom" type="file"> <driver name="qemu" type="raw" /> <target bus="ide" dev="hdb" /> <readonly /> <address bus="0" controller="0" target="0" type="drive" unit="1" /> </disk> <controller index="0" model="ich9-ehci1" type="usb"> <address bus="0x00" domain="0x0000" function="0x7" slot="0x04" type="pci" /> </controller> <controller index="0" model="ich9-uhci1" type="usb"> <master startport="0" /> <address bus="0x00" domain="0x0000" function="0x0" multifunction="on" slot="0x04" type="pci" /> </controller> <controller index="0" model="ich9-uhci2" type="usb"> <master startport="2" /> <address bus="0x00" domain="0x0000" function="0x1" slot="0x04" type="pci" /> </controller> <controller index="0" model="ich9-uhci3" type="usb"> <master startport="4" /> <address bus="0x00" domain="0x0000" function="0x2" slot="0x04" type="pci" /> </controller> <controller index="0" model="pci-root" type="pci" /> <controller index="0" type="ide"> <address bus="0x00" domain="0x0000" function="0x1" slot="0x01" type="pci" /> </controller> <interface type="network"> <mac address="52:54:00:e8:56:bb" /> <source network="default" /> <model type="rtl8139" /> <address bus="0x00" domain="0x0000" function="0x0" slot="0x03" type="pci" /> </interface> <serial type="pty"> <target port="0" type="isa-serial"> <model name="isa-serial" /> </target> </serial> <console type="pty"> <target port="0" type="serial" /> </console> <input bus="ps2" type="mouse" /> <input bus="ps2" type="keyboard" /> <graphics autoport="yes" listen="0.0.0.0" port="-1" type="vnc"> <listen address="0.0.0.0" type="address" /> </graphics> <video> <model heads="1" primary="yes" type="cirrus" vram="16384" /> <address bus="0x00" domain="0x0000" function="0x0" slot="0x02" type="pci" /> </video> <memballoon model="virtio"> <address bus="0x00" domain="0x0000" function="0x0" slot="0x05" type="pci" /> </memballoon> </devices> </domain>
xml即可扩展标记语言,它可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。从结构上,很像HTML超文本标记语言。但他们被设计的目的是不同的,超文本标记语言被设计用来显示数据,其焦点是数据的外观。它被设计用来传输和存储数据,其焦点是数据的内容。
xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml
一、创建:
ElementTree(tag),其中tag表示根节点,初始化一个ElementTree对象。
Element(tag, attrib={}, **extra)函数用来构造XML的一个根节点,其中tag表示根节点的名称,attrib是一个可选项,表示节点的属性。
SubElement(parent, tag, attrib={}, **extra)用来构造一个已经存在的节点的子节点 Element.text和SubElement.text表示element对象的额外的内容属性,Element.tag和Element.attrib分别表示element对象的标签和属性。
ElementTree.write(file, encoding='us-ascii', xml_declaration=None, default_namespace=None, method='xml'),函数新建一个XML文件,并且将节点数数据写入XML文件中。
下面以新建一个网站的sitemap.xml文件为例进行代码示例:
#Author:Anliu from xml.etree import ElementTree as ET def build_sitemap(): urlset = ET.Element("urlset") #设置一个根节点,标签为urlset url = ET.SubElement(urlset,"url") loc = ET.SubElement(url,"loc") #在根节点urlset下建立子节点 loc.text = "http://www/baidu.com" lastmod = ET.SubElement(url,"lastmod") lastmod.text = "2020-4-13" changefreq = ET.SubElement(url,"changefreq") changefreq.text = "daily" priority = ET.SubElement(url,"priority") priority.text = "1.0" tree = ET.ElementTree(urlset) tree.write("test_xml.xml") if __name__ == '__main__': build_sitemap()
结果如下图所示:
二、遍历:
#Author:Anliu import xml.etree.cElementTree as ET tree = ET.parse("centos7u4.xml") root = tree.getroot() #rint(root.tag) #rint(root.text) #遍历xml文档 #for child in root: # print(child.tag, child.attrib) # for i in child: # print(i.tag, i.text) # 只遍历 节点 for node in root.iter('name'): print(node.tag, node.text) node.text = "Centos7u4-1" #将name字段改成Centos7u4-1 print(node.tag, node.text) tree.write("centos7u4.xml")
三、修改:
ElementTree.parse(source, parser=None),将xml文件加载并返回ElementTree对象。parser是一个可选的参数,如果为空,则默认使用标准的XMLParser解析器。
ElementTree.getroot(),得到根节点。返回根节点的element对象。
Element.remove(tag),删除root下名称为tag的子节点 以下函数,ElementTree和Element的对象都包含。
find(match),得到第一个匹配match的子节点,match可以是一个标签名称或者是路径。返回个element findtext(match,default=None),得到第一个配置的match的element的内容 findall(match),得到匹配match下的所有的子节点,match可以是一个标签或者是路径,它会返回一个list,包含匹配的elements的信息 iter(tag),创建一个以当前节点为根节点的iterator。
示例1:将1小节创建的sitemap.xml的url修改为“www.baidu.com”
#Author:Anliu from xml.etree import ElementTree as ET tree = ET.parse("test_xml.xml") url = tree.find("url") for rank in tree.iter('loc'): rank.text = "http://www.baidu.com" tree.write("test_xml.xml")
示例2:将centos7u4中的vcpu个数加1
#Author:Anliu import xml.etree.ElementTree as ET tree = ET.parse("centos7u4.xml") root = tree.getroot() # 修改:将centos7u4中的cup个数加1,并另存为xmltest.xml for node in root.iter('vcpu'): # print(node) new_year = int(node.text) + 1 node.text = str(new_year) node.set("updated", "yes") tree.write("xmltest.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>
删除node
import
xml.etree.ElementTree as ET
tree
=
ET.parse(
"xmltest.xml"
)
root
=
tree.getroot()
for country in root.findall('country'): rank = int(country.find('rank').text) if rank > 50: root.remove(country) tree.write('output.xml')