第16题: 将 第 0013 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示:
<?xmlversion="1.0" encoding="UTF-8"?>
<root>
<citys>
<!--
城市信息
-->
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
</citys>
</root>
#!/usr/bin/env python3
# -*- coding : utf-8 -*-
from pyexcel_xls import get_data
from xml.dom.minidom import Document
import json
data = get_data('city.xls')
doc = Document()
root_node = doc.createElement("root")
doc.appendChild(root_node)
stu_node = doc.createElement("citys")
root_node.appendChild(stu_node)
stu_node.appendChild(doc.createComment('\n\t城市信息\n'))
citystr = "{\n"
for ele in data['city']:
for i in range(len(ele)):
if i == 0:
citystr += "\t"
citystr += "\"" + str(ele[i]) + "\""
citystr += " : "
else:
citystr += "\"" + str(ele[i]) + "\""
citystr += "\n"
citystr += "}"
print(citystr)
dic_node = doc.createTextNode(citystr)
stu_node.appendChild(dic_node)
with open("city.xml",'wb') as s:
s.write(doc.toprettyxml(indent = "",newl = "\n",encoding = "utf-8"))