xml 是一种特殊的格式,用来表示和存储数据。
- 存储数据,用户存储配置。Java中居多
- 传输输出,将数据嵌套到标签中。
直接上python代码
import xml.etree.ElementTree as ET
import json
text = '''
<data>
<country name="example1">
<rank updated="yes">1</rank>
<people>555555</people>
</country>
<country name="example2">
<rank updated="yes">2</rank>
<people>4343</people>
</country>
</data>
'''
root = ET.fromstring(text)
dictionary = {}
for nodeIdx in range(len(root)):
current = {}
for nodeItemIdx in range(len(root[nodeIdx])):
current[root[nodeIdx][nodeItemIdx].tag] = root[nodeIdx][nodeItemIdx].text
dictionary[nodeIdx] = current
print(json.dumps(dictionary, indent=2))
# 结果如下
{
"0": {
"rank": "1",
"people": "555555"
},
"1": {
"rank": "2",
"people": "4343"
}
}
给个赞再走呗~