如何使用Python更改给定XML的元素特有名称

时间:2018-10-09 03:06:22

标签: python xml

这是输入XML文件input.xml

<edges>
  <edge from_="4" id_="488909c258004c9197c95f6f66bfec25" numLanes="3" to="0"/>
  <edge from_="1" id_="54e2c0a3f63b4e869fb6c62c0255ac9c" numLanes="3" to="0"/>
  <edge from_="3" id_="fc4d9a75d45343a4a2523c713f5e727c" numLanes="5" to="0"/>
  <edge from_="2" id_="2bf32d7d7b1545d58ea945a0c8a6e9a3" numLanes="3" to="0"/>
  <edge from_="0" id_="671fe5d822ba4093b2d5163708378f19" numLanes="3" to="4"/>
  <edge from_="0" id_="9ca39900ffe44887838f1f77edaedb90" numLanes="3" to="1"/>
  <edge from_="0" id_="d5435e6103b845a28b9a4c2ae1df2f02" numLanes="3" to="3"/>
  <edge from_="0" id_="2573a935d265469995d647fa5fe82599" numLanes="3" to="2"/>
</edges>

我要做的是将每个edge元素的属性名称from_更改为from,将id_更改为id。因此输出应如下所示:

<edges>
  <edge from="4" id="488909c258004c9197c95f6f66bfec25" numLanes="3" to="0"/>
  <edge from="1" id="54e2c0a3f63b4e869fb6c62c0255ac9c" numLanes="3" to="0"/>
  <edge from="3" id="fc4d9a75d45343a4a2523c713f5e727c" numLanes="5" to="0"/>
  <edge from="2" id="2bf32d7d7b1545d58ea945a0c8a6e9a3" numLanes="3" to="0"/>
  <edge from="0" id="671fe5d822ba4093b2d5163708378f19" numLanes="3" to="4"/>
  <edge from="0" id="9ca39900ffe44887838f1f77edaedb90" numLanes="3" to="1"/>
  <edge from="0" id="d5435e6103b845a28b9a4c2ae1df2f02" numLanes="3" to="3"/>
  <edge from="0" id="2573a935d265469995d647fa5fe82599" numLanes="3" to="2"/>
</edges>

我尝试通过xml.etree.ElementTree访问元素的属性,但是不知道如何更改它:

import xml.etree.ElementTree as et
with open('input.xml', encoding = 'utf-8') as f:
    tree = et.parse(f)
    root= tree.getroot()
    for edge in root:
        print(list(edge.attrib.keys())[:2])

如何使用Python3做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以将内置xml模块用于类似这样的简单操作。例如,使用ipython会话将数据加载到ElementTree中:

In [35]: from xml.etree import ElementTree
In [36]: x = """
    ...: <edges>
    ...:   <edge from_="4" id_="488909c258004c9197c95f6f66bfec25" numLanes="3" to="0"/>
    ...:   <edge from_="1" id_="54e2c0a3f63b4e869fb6c62c0255ac9c" numLanes="3" to="0"/>
    ...:   <edge from_="3" id_="fc4d9a75d45343a4a2523c713f5e727c" numLanes="5" to="0"/>
    ...:   <edge from_="2" id_="2bf32d7d7b1545d58ea945a0c8a6e9a3" numLanes="3" to="0"/>
    ...:   <edge from_="0" id_="671fe5d822ba4093b2d5163708378f19" numLanes="3" to="4"/>
    ...:   <edge from_="0" id_="9ca39900ffe44887838f1f77edaedb90" numLanes="3" to="1"/>
    ...:   <edge from_="0" id_="d5435e6103b845a28b9a4c2ae1df2f02" numLanes="3" to="3"/>
    ...:   <edge from_="0" id_="2573a935d265469995d647fa5fe82599" numLanes="3" to="2"/>
    ...: </edges>
    ...: """


In [37]: elements = ElementTree.fromstring(x)
In [38]: for eachedge in elements:
    ...:     eachedge.attrib['id'] = eachedge.attrib['id_']
    ...:     eachedge.attrib['from'] = eachedge.attrib['from_']
    ...:     del eachedge.attrib['id_']
    ...:     del eachedge.attrib['from_']
    ...:
结果:
In [39]: print(etree.ElementTree.tostring(elements).decode())
<edges>
  <edge from="4" id="488909c258004c9197c95f6f66bfec25" numLanes="3" to="0" />
  <edge from="1" id="54e2c0a3f63b4e869fb6c62c0255ac9c" numLanes="3" to="0" />
  <edge from="3" id="fc4d9a75d45343a4a2523c713f5e727c" numLanes="5" to="0" />
  <edge from="2" id="2bf32d7d7b1545d58ea945a0c8a6e9a3" numLanes="3" to="0" />
  <edge from="0" id="671fe5d822ba4093b2d5163708378f19" numLanes="3" to="4" />
  <edge from="0" id="9ca39900ffe44887838f1f77edaedb90" numLanes="3" to="1" />
  <edge from="0" id="d5435e6103b845a28b9a4c2ae1df2f02" numLanes="3" to="3" />
  <edge from="0" id="2573a935d265469995d647fa5fe82599" numLanes="3" to="2" />
</edges>