gpt4 book ai didi

Python XML 添加元素到文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:57:28 24 4
gpt4 key购买 nike

我有大约 1000 个 xml 文件,其中一些缺少元素。我编写了一个脚本来搜索 xml 并打印元素。我想添加在元素不存在时添加该元素的功能,但没有成功。

如下所示,DVT3 不存在,需要添加。

我的代码

XMLParser = etree.XMLParser(remove_blank_text=True)
for f in os.listdir(directory):
if f.endswith(".xml"):

xmlfile = directory + '/' + f


tree = etree.parse(xmlfile, parser=XMLParser)
root = tree.getroot()

hardwareRevisionNode = root.find(".//hardwareRevision")

try:
print f + ' : ' + hardwareRevisionNode.text
except Exception as e:
print str(e)
print xmlfile
#Wearable = root.find(".//Wearable")
ChildNode = etree.Element(".//Wearable")
ChildNode.text = "DVT2"
ChildNode.append(ChildNode)
tree.write(xmlfile, pretty_print=True)

XML 文件

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Speech>
<Results>
<breakpoint name="ASR_START_RECOGNITION" elapsedTime="00:00:00.000" />
</Results>
<Meta>
<Dialog>
<sessionUUID>7c9b1e3a-b22f-4793-818f-72bc6e7b84a9</sessionUUID>
</Dialog>
<ASR>
<engine>
<name>Rockhopper</name>
<version>1.0.0.61</version>
</engine>
<wrapper>
<name>RockhopperAsrEngine</name>
<version>1.8.2</version>
</wrapper>
<wrapper>
<name>Core</name>
<version>1.8.2</version>
</wrapper>
<resource>
<name>Language Model</name>
<version>1.4.4</version>
</resource>
</ASR>
<Application>
<name>FightClub</name>
<version>0.1.550</version>
<commit>8f7a411</commit>
<buildDate>2016-03-09T18:16Z</buildDate>
<branch>HEAD</branch>
</Application>
<Wearable>
<firmware>1.0.183 - FCB1APP000-1611W0183</firmware>
</Wearable>
</Meta>
</Speech>

我想要的 XML 文件

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Speech>
<Results>
<breakpoint name="ASR_START_RECOGNITION" elapsedTime="00:00:00.000" />
</Results>
<Meta>
<Dialog>
<sessionUUID>7c9b1e3a-b22f-4793-818f-72bc6e7b84a9</sessionUUID>
</Dialog>
<ASR>
<engine>
<name>Rockhopper</name>
<version>1.0.0.61</version>
</engine>
<wrapper>
<name>RockhopperAsrEngine</name>
<version>1.8.2</version>
</wrapper>
<wrapper>
<name>Core</name>
<version>1.8.2</version>
</wrapper>
<resource>
<name>Language Model</name>
<version>1.4.4</version>
</resource>
</ASR>
<Application>
<name>FightClub</name>
<version>0.1.550</version>
<commit>8f7a411</commit>
<buildDate>2016-03-09T18:16Z</buildDate>
<branch>HEAD</branch>
</Application>
<Wearable>
<firmware>1.0.183 - FCB1APP000-1611W0183</firmware>
<hardwareRevision>DVT3</hardwareRevision>
</Wearable>
</Meta>
</Speech>

最佳答案

You can try out xmltodict.

import xmltodict as x

with open(myfile) as f:

xmlDictionary=x.parse(f.read(),'utf-8')
xmlDictionary['Speech']['Meta']['Wearable'].update({"hardwareRevision": "DVT3"})

output = x.unparse(xmlDictionary)

with open(outfile,'w') as out:
out.write(output)

如果您愿意,可以让它并行运行,如果存储是一个问题,只需替换文件内容(或在创建新文件后立即删除旧文件)。

关于Python XML 添加元素到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40661948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com