我正在使用 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>
我需要这种方式的输出......帮帮我!提前致谢!