python_xml文件的解析与构建

来源:互联网 发布:excel数据分析功能 编辑:程序博客网 时间:2024/06/14 06:05

官方文档:https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

解析xml文档:将xml文档解析,获取文档中的内容
采用xml.tree.ElementTree模块的parse函数

from xml.etree.ElementTree import parsepath = r'E:\settings.xml'f = open(path)tree= parse(path)       #创建一个xml对象root = tree.getroot()   #获得结构树根节点print('输出根节点的标签:%s'%root.tag) #.tag获取标签值print('输出根节点的属性:%s'%root.attrib)  #.attrib获取属性值#获取root下的子节点for child in root:    print('子节点标签:%s,子节点属性%s:'%(child.tag,child.attrib))#只能找出root下的第一级子节点print('root下第一个子节点下的第二个子节点的内容%s:'%root[0][1].text)print('输出所以结点内容%s:'%list(root.iter()))print('输出所有节点为neighbor的节点%s:'%list(root.iter('neighbor')))#root.iter('neighbor')可进行迭代,获取其属性、标签等内容for x in root.iter('neighbor'):    print('子节点neighbor的属性%s'%x.attrib)#find,findall方法for country in root.findall('country'):    rank = country.find('rank').text    name = country.get('name')    print(rank,name)#set方法,设置属性for rank in root.iter('rank'):    newrank = int(rank.text)+1    rank.text = str(newrank)    rank.set('updated','yes') tree.write(path)print('update success')print('neighbor节点direction属性等于w的节点%s'%root.findall('.//neighbor[@direction="W"]'))#采用[@attrib]

官方文档中还提及到一些查找节点元素的方法如下:

import xml.etree.ElementTree as ETroot = ET.fromstring(countrydata)# Top-level elementsroot.findall(".")# All 'neighbor' grand-children of 'country' children of the top-level# elementsroot.findall("./country/neighbor")# Nodes with name='Singapore' that have a 'year' childroot.findall(".//year/..[@name='Singapore']")# 'year' nodes that are children of nodes with name='Singapore'root.findall(".//*[@name='Singapore']/year")# All 'neighbor' nodes that are the second child of their parentroot.findall(".//neighbor[2]")

构建xml文档:将某些文件用xml格式文件表示。使用xml.etree.ElementTree模块的ElementTree,Element

import csvfrom xml.etree.ElementTree import ElementTree,Elementdef pretty(e,level=0):    if len(e)>0:        e.text = '\n'+'\t'*(level+1)        for child in e:            pretty(child,level+1)        child.tail = child.tail[:-1]    e.tail='\n'+'\t'*level#将一个csv文件,构建为xml文档def csvToXml(csv_file):    with open(csv_file,'r') as f:        reader = csv.reader(f) #创建一个csv读取对象        header = next(reader)#获取第一行列名字段        print(header)        root = Element('Data')#创建根节点        for row in reader:            eRow = Element('Row')#创建第一层子节点            root.append(eRow)#将eRow子节点加到root节点下            for tag,text in zip(header,row):                eCont = Element(tag)#创建第二层子节点元素                eCont.text = text                eRow.append(eCont)    pretty(root)#pretty方法用于美化构建的xml文件    return ElementTree(root)#将xml格式内容写入文件                path = r'E:\xxx.csv'et=csvToXml(path)xml_path = r'E:\xxx.xml'et.write(xml_path,encoding='utf-8')#采用write方法将xml写入文件#添加encoding='utf-8'使中文字符正常显示
原创粉丝点击