Show me the code之Python练习册 Q17~19 xml操作

__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')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值