【发布时间】:2013-04-29 07:47:31
【问题描述】:
我正在使用 xml.ElementTree 循环遍历 python 列表并将其写入树结构中的 xml 文件。这是以下代码并遵循所需的输出。有谁能帮帮我!!
import xml.etree.ElementTree as ET
sample = ['germany','India','USA','srilanka']
root = ET.Element("root")
data = ET.SubElement(root, "data")
title = ET.SubElement(data, "country")
for a in sample:
title.text = a
data.append('title')
tree = ET.ElementTree(root)
tree.write("page.xml")
电流输出
- <root>
<data>
<country>srilanka</country>
<country>srilanka</country>
<country>srilanka</country>
<country>srilanka</country>
<country>srilanka</country>
</data>
</root>
Expected output
<root>
<data>
<country>germany</country>
<country>india</country>
<country>usa</country>
<country>srilanka</country>
</data>
</root>
我需要这种方式的输出...帮帮我!! 提前致谢!
【问题讨论】:
标签: xml parsing python-2.7 elementtree