python小程序-0016

来源:互联网 发布:淘宝网如何注册网店 编辑:程序博客网 时间:2024/05/16 17:34

第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_datafrom xml.dom.minidom import Documentimport jsondata = 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"))
原创粉丝点击