Show me the code之Python练习册 Q17~19 xml操作
2016-04-18 09:26
615 查看
__file__ = 'A17.py' __author__ = 'Jerry Liu' __date__ = '2016-04-15' """ 问题: 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如下所示: <?xml version="1.0" encoding="UTF-8"?> <root> <students> <!-- 学生信息表 "id" : [名字, 数学, 语文, 英文] --> { "1" : ["张三", 150, 120, 100], "2" : ["李四", 90, 99, 95], "3" : ["王五", 60, 66, 68] } </students> </root> """ from openpyxl.reader.excel import load_workbook from xml.dom.minidom import Document def getdata(path): wb = load_workbook(path) # 读取第一个sheet snames = wb.get_sheet_names() sheet = wb.get_sheet_by_name(snames[0]) # 创建xml doc = Document() # 创建根元素 root = doc.createElement('root') doc.appendChild(root) # 创建学生列表元素 stus = doc.createElement('students') root.appendChild(stus) for line in range(sheet.get_highest_row()): line += 1 # 创建学生元素 stu = doc.createElement('student') stus.appendChild(stu) # 创建学生姓名元素 name = sheet.cell(row=line, column=1).value stu_name = doc.createElement('Name') stu_name.appendChild(doc.createTextNode(name)) stu.appendChild(stu_name) # 创建英文成绩 yy_value = sheet.cell(row=line, column=2).value yy_name = doc.createElement('English') yy_name.appendChild(doc.createTextNode(str(yy_value))) stu.appendChild(yy_name) # 创建语文成绩 yw_value = sheet.cell(row=line, column=3).value yw_name = doc.createElement('Chinese') yw_name.appendChild(doc.createTextNode(str(yw_value))) stu.appendChild(yw_name) # 数学语文成绩 sx_value = sheet.cell(row=line, column=4).value sx_name = doc.createElement('Math') sx_name.appendChild(doc.createTextNode(str(sx_value))) stu.appendChild(sx_name) # 保存文件 f = open("d://student.xml", "w") doc.writexml(f) f.close() if __name__ == '__main__': getdata('d://student.xlsx')
__file__ = 'A18.py' __author__ = 'Jerry Liu' __date__ = '2016-04-15' """ 问题:将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示: <?xmlversion="1.0" encoding="UTF-8"?> <root> <citys> <!-- 城市信息 --> { "1" : "上海", "2" : "北京", "3" : "成都" } </citys> </root> """ from openpyxl.reader.excel import load_workbook from xml.dom.minidom import Document def getdata(path): wb = load_workbook(path) # 读取第一个sheet snames = wb.get_sheet_names() sheet = wb.get_sheet_by_name(snames[0]) cxml = Document() eRoot =cxml.createElement('root') cxml.appendChild(eRoot) eCities = cxml.createElement('cities') eRoot.appendChild(eCities) eComments = cxml.createComment('城市信息') eCities.appendChild(eComments) # 定义字典 dict = {} for line in range(sheet.get_highest_row()): line += 1 id = sheet.cell(row=line, column=1).value city = sheet.cell(row=line, column=2).value dict[id] = city eCities.appendChild(cxml.createTextNode(str(dict))) f = open('d://city.xml', 'w') cxml.writexml(f) f.close() if __name__ == '__main__': getdata('d://city.xlsx')
__file__ = 'A19.py' __author__ = 'Jerry Liu' __date__ = '2016-04-18' """ 问题:将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中,如下所示: <?xml version="1.0" encoding="UTF-8"?> <root> <numbers> <!-- 数字信息 --> [ [1, 82, 65535], [20, 90, 13], [26, 809, 1024] ] </numbers> </root> """ from openpyxl.reader.excel import load_workbook from xml.dom.minidom import Document def getdata(path): wb = load_workbook(path) # 读取第一个sheet snames = wb.get_sheet_names() sheet = wb.get_sheet_by_name(snames[0]) mdata = [] #读取数据 for line in range(sheet.get_highest_row()): line += 1 list = [] list.append(sheet.cell(row=line, column=1).value) list.append(sheet.cell(row=line, column=2).value) list.append(sheet.cell(row=line, column=3).value) mdata.append(list) cxml = Document() eRoot =cxml.createElement('root') cxml.appendChild(eRoot) eNums = cxml.createElement('numbers') eRoot.appendChild(eNums) eComments = cxml.createComment('数字信息') eNums.appendChild(eComments) eData = cxml.createTextNode(str(mdata)) eNums.appendChild(eData) f = open('d://numbers.xml', 'w') cxml.writexml(f) f.close() if __name__ == '__main__': getdata('d://numbers.xlsx')
相关文章推荐
- Python 中的 property 属性
- numpy 常用 api(四)
- Python反射
- 【python学习】装饰器@
- Python读写CSV数据
- Python实现递归遍历文件夹并删除文件
- python代码片段【这些都是可以work的】
- 实用的 Python —— 进制(二进制形式中 1 的位数)
- Python tools for Visual Studio插件介绍
- 初试主题模型LDA-基于python的gensim包
- Python学习路程day12
- Python 练习实例37
- Python的__slots__
- Python实现递归遍历文件夹并删除文件
- python实现按行切分文本文件的方法
- python动态加载包的方法小结
- python UNIX_TIMESTAMP时间处理方法分析
- python使用paramiko实现远程拷贝文件的方法
- Python Sql数据库增删改查操作简单封装
- 利用Python获取赶集网招聘信息前篇