【发布时间】:2013-12-25 21:34:52
【问题描述】:
我有以下 xml:
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria"/>
<neighbor direction="W" name="Switzerland"/>
</country>
我想将值“列支敦士登”替换为“德国”,因此结果应如下所示:
<country name="Germany">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria"/>
<neighbor direction="W" name="Switzerland"/>
</country>
到目前为止,我已经到了这一点:
from xml.dom import minidom
xmldoc = minidom.parse('C:/Users/Torah/Desktop/country.xml')
print xmldoc.toxml()
country = xmldoc.getElementsByTagName("country")
firstchild = country[0]
print firstchild.attributes["name"].value
#simple string mathod to replace
print firstchild.attributes["name"].value.replace("Liechtenstein", "Germany")
print xmldoc.toxml()
【问题讨论】:
-
到目前为止你有什么?
-
我可以访问该值,但不知道如何替换它。
-
最好包含您拥有的代码,以便可以对其进行更新/修改以替换该值。
标签: python xml replace minidom