美文网首页我爱编程
Python 练习册 0017、0018、0019题 (xls转

Python 练习册 0017、0018、0019题 (xls转

作者: 海上牧云l | 来源:发表于2017-04-24 21:13 被阅读135次

    第 0017 题: 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如

    下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <students>

    {
    "1" : ["张三", 150, 120, 100],
    "2" : ["李四", 90, 99, 95],
    "3" : ["王五", 60, 66, 68]
    }
    </students>
    </root>

    import xlrd
    from xml.etree.ElementTree import Element, SubElement, Comment, ElementTree
    
    
    wb = xlrd.open_workbook(r'file\stu.xls')
    sh = wb.sheet_by_index(0)
    
    data = dict()
    for rx in range(sh.nrows):
        row = sh.row(rx)
        value_list = list()
        key = row[0].value
        for i in row[1:]:
            value = i.value
            value_list.append(value)
    
        data[key] = value_list
    print(data)
    
    root = Element('root')
    comment = Comment('学生信息表"id" : [名字, 数学, 语文, 英文]')
    root.append(comment)
    child = SubElement(root, 'students')
    child.text = str(data)
    tree = ElementTree(root)
    tree.write('file\\student22.xml', encoding='utf8')
    

    第 0018 题: 将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示:

    <?xmlversion="1.0" encoding="UTF-8"?>
    <root>
    <citys>

    {
    "1" : "上海",
    "2" : "北京",
    "3" : "成都"
    }
    </citys>
    </root>

    import xlrd
    from xml.etree.cElementTree import Element, ElementTree, Comment, SubElement
    
    wb = xlrd.open_workbook('file\\city.xls')
    ws = wb.sheet_by_index(0)
    data = dict()
    for rx in range(ws.nrows):
        row = ws.row(rx)
        key = row[0].value
        value = row[1].value
        data[key] = value
    
    root = Element('root')
    comment = Comment('城市信息')
    root.append(comment)
    child = SubElement(root, 'citys')
    child.text = str(data)
    tree = ElementTree(root)
    tree.write('file\\city.xml', encoding='utf8')
    

    第 0019 题: 将 第 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>

    import xlrd
    from xml.etree.cElementTree import Element, ElementTree, SubElement, Comment
    
    wb = xlrd.open_workbook('file\\num.xls')
    ws = wb.sheet_by_index(0)
    content = list()
    for xr in range(ws.nrows):
        row = ws.row(xr)
        num_list = list()
        for i in row:
            value = i.value
            num_list.append(value)
        content.append(num_list)
    print(content)
    
    root = Element('root')
    comment = Comment('数字信息')
    root.append(comment)
    child = SubElement(root, 'numbers')
    child.text = str(content)
    tree = ElementTree(root)
    tree.write('file\\num.xml', encoding='utf8')
    
    

    相关文章

      网友评论

        本文标题:Python 练习册 0017、0018、0019题 (xls转

        本文链接:https://www.haomeiwen.com/subject/boxmzttx.html