Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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元素树赢得';解析后不更新新文件_Python_Xml_Xml Parsing_Elementtree - Fatal编程技术网

Python元素树赢得';解析后不更新新文件

Python元素树赢得';解析后不更新新文件,python,xml,xml-parsing,elementtree,Python,Xml,Xml Parsing,Elementtree,使用ElementTree解析XML中的属性值,并编写一个新的XML文件。它将控制台新的更新值并写入一个新文件。但不会更新新文件中的任何更改。请帮助我理解我做错了什么。下面是XML和Python代码: XML <?xml version="1.0"?> <!-- --> <req action="get" msg="1" rank="1" rnklst="1" runuf="0" status="1" subtype="list" type="60" uni

使用
ElementTree
解析XML中的属性值,并编写一个新的XML文件。它将控制台新的更新值并写入一个新文件。但不会更新新文件中的任何更改。请帮助我理解我做错了什么。下面是XMLPython代码:

XML

<?xml version="1.0"?>
<!--
-->
    <req action="get" msg="1" rank="1" rnklst="1" runuf="0" status="1" subtype="list" type="60" univ="IL" version="fhf.12.000.00" lang="ENU" chunklimit="1000" Times="1">
        <flds>
            <f i="bond(long) hff"  aggregationtype="WeightedAverage" end="2016-02-29" freq="m" sid="fgg" start="2016-02-29"/>
            <f i="bond(short) ggg"  aggregationtype="WeightedAverage" end="2016-02-29" freq="m" sid="fhf" start="2016-02-29"/>
        </flds>
    <dat>
        <r i="hello" CalculationType="3" Calculate="1" />
    </dat>
</req>

当您获取属性
i
,并将其分配给
port\u id
时,您只需要一个常规的Python字符串。对它调用replace只是Python字符串
.replace()
方法

要使用etree节点的方法:

for node in tree.iter('r'):
    node.set('i', "new")
    print node
for node in tree.iter('r'):
    node.set('i', "new")
    print node