Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在python上替换循环列表中的xml元素?_Python 3.x_Xml_Parsing_Xml Parsing - Fatal编程技术网

Python 3.x 如何在python上替换循环列表中的xml元素?

Python 3.x 如何在python上替换循环列表中的xml元素?,python-3.x,xml,parsing,xml-parsing,Python 3.x,Xml,Parsing,Xml Parsing,我有一个xml树,我想替换xml结构中的子元素。 这是从文件中读取的实际xml树 xml_data=ET.parse('file1.xml') 我希望循环列表时使用这种格式 a = [1,23453, 3543,4354,3455, 6345] <?xml version='1.0' encoding='UTF-8'?> <call method="xxxx" callerName="xxx"> <credenti

我有一个xml树,我想替换xml结构中的子元素。 这是从文件中读取的实际xml树

xml_data=ET.parse('file1.xml')


我希望循环列表时使用这种格式

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>
a=[1234533543456345]

xml解析新手。任何帮助都将不胜感激。提前感谢

我是python新手,但这是工作。我只发现了一个bug: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)
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)