[英]How to parse and modify xml data using python lxml
I need to modify the XML tag values using lxml (Author and Description) by parsing through xml files. 我需要通过解析xml文件,使用lxml(作者和描述)修改XML标记值。 below is the input file I am using and the output file I need. 下面是我正在使用的输入文件和我需要的输出文件。 Below is the code I am using: 以下是我使用的代码:
input xml file: 输入xml文件:
<Summary>
<Author>ABC</Author>
<Description>ABC DATA</Description>
<Function>24</Function>
</Summary>
Required output file: 必需的输出文件:
<Summary>
<Author>DEF</Author>
<Description>DEF DATA</Description>
<Function>24</Function>
</Summary>
from lxml import etree
root = etree.parse(r"C:\Users\input\input.xml")
for elem in root.xpath('.//Author'):
elem.text = "DEF"
root.write("output.xml", pretty_print=True,xml_declaration=True,encoding="UTF-8")
This should work 这应该工作
import xml.etree.ElementTree as ET
xml = '''<root>
<Summary>
<Author>ABC</Author>
<Description>ABC DATA</Description>
<Function>24</Function>
</Summary>
<Summary>
<Author>ABC</Author>
<Description>ABC DATA</Description>
<Function>24</Function>
</Summary>
</root>'''
tree = ET.fromstring(xml)
for author in tree.findall('.//Summary/Author'):
author.text = 'new author value goes here'
for desc in tree.findall('.//Summary/Description'):
desc.text = 'new desc value goes here'
ET.dump(tree)
# call the line below if you need to save to a file
# tree.write(open('new_file.xml', 'w'))
output 产量
<root>
<Summary>
<Author>new author value goes here</Author>
<Description>new desc value goes here</Description>
<Function>24</Function>
</Summary>
<Summary>
<Author>new author value goes here</Author>
<Description>new desc value goes here</Description>
<Function>24</Function>
</Summary>
</root>
If you are only looking to replace each occurance of "ABC" with "DEF" but otherwise leave the text as-is, this should do it: 如果您只想用“DEF”替换每个“ABC”的出现,但是保留文本原样,这应该这样做:
dat = [your input above]
nodes = ['Author','Description']
for node in nodes:
for elem in root.xpath(f'.//{node}'):
elem.text = elem.text.replace("ABC","DEF")
The output is your desired output. 输出是您想要的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.