如何从 python 的循环列表中替换 xml 元素?
How to replace xml element from looping list on python?
我有一个 xml 树,我想替换 xml 结构中的子元素。
这是从文件
中读取的实际 xml 树
xml_data = ET.parse('file1.xml')
<?xml version='1.0' encoding='UTF-8'?>
<call method="xxxx" callerName="xxx">
<credentials login="" password=""/>
<filters>
<accounts>
<account code="" ass="" can=""/>
</accounts>
</filters>
</call>
我希望循环列表采用这种格式
a = [1,23453, 3543,4354,3455, 6345]
<?xml version='1.0' encoding='UTF-8'?>
<call method="xxxx" callerName="xxx">
<credentials login="" password=""/>
<filters>
<accounts>
<account code="1" ass="34" can="yes"/>
<account code="23453" ass="34" can="yes"/>
<account code="3543" ass="34" can="yes"/>
<account code="4354" ass="34" can="yes"/>
<account code="3455" ass="34" can="yes"/>
<account code="6345" ass="34" can="yes"/>
</accounts>
</filters>
</call>
xml-解析的新手。任何帮助,将不胜感激。提前致谢
我是 python 的新人,但它的工作。我只发现 1 个错误:a 的 len 必须等于 len。如果我能帮助你,那就太好了。祝你好运。
from xml.etree import ElementTree as ET
a = [1, 23453, 3543, 4354, 3455, 6345]
code = 0
xmlfile = "./log/logs.xml"
tree = ET.parse(xmlfile)
root = tree.getroot()
for filters in root.findall("filters"):
for accounts in filters.findall("accounts"):
for account in accounts.findall("account"):
attributes = account.attrib
attributes["code"] = str(a[code])
attributes["ass"] = "34"
attributes["can"] = "yes"
code += 1
tree.write(xmlfile)
我有一个 xml 树,我想替换 xml 结构中的子元素。 这是从文件
中读取的实际 xml 树xml_data = ET.parse('file1.xml')
<?xml version='1.0' encoding='UTF-8'?>
<call method="xxxx" callerName="xxx">
<credentials login="" password=""/>
<filters>
<accounts>
<account code="" ass="" can=""/>
</accounts>
</filters>
</call>
我希望循环列表采用这种格式
a = [1,23453, 3543,4354,3455, 6345]
<?xml version='1.0' encoding='UTF-8'?>
<call method="xxxx" callerName="xxx">
<credentials login="" password=""/>
<filters>
<accounts>
<account code="1" ass="34" can="yes"/>
<account code="23453" ass="34" can="yes"/>
<account code="3543" ass="34" can="yes"/>
<account code="4354" ass="34" can="yes"/>
<account code="3455" ass="34" can="yes"/>
<account code="6345" ass="34" can="yes"/>
</accounts>
</filters>
</call>
xml-解析的新手。任何帮助,将不胜感激。提前致谢
我是 python 的新人,但它的工作。我只发现 1 个错误:a 的 len 必须等于 len。如果我能帮助你,那就太好了。祝你好运。
from xml.etree import ElementTree as ET
a = [1, 23453, 3543, 4354, 3455, 6345]
code = 0
xmlfile = "./log/logs.xml"
tree = ET.parse(xmlfile)
root = tree.getroot()
for filters in root.findall("filters"):
for accounts in filters.findall("accounts"):
for account in accounts.findall("account"):
attributes = account.attrib
attributes["code"] = str(a[code])
attributes["ass"] = "34"
attributes["can"] = "yes"
code += 1
tree.write(xmlfile)