• python36--将数据保存为excel


    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import xlwt
    import os
    
    
    class ExcelHelper(object):
        @staticmethod
        def create_excel(column_heads, row_items, file_path):
            book = xlwt.Workbook(encoding='utf-8', style_compression=0)
            sheet = book.add_sheet('sheet1', cell_overwrite_ok=True)
            column_count = len(column_heads)
            row_count = len(row_items)
            # 写入列标题
            for column_id in range(column_count):
                sheet.write(0, column_id, column_heads[column_id])
            # 遍历所有行
            for row_id in range(row_count):
                row_item = row_items[row_id]
                # 遍历每行的所有列
                for column_id in range(column_count):
                    # 将每一行的对应列写sheet中
                    sheet.write(row_id + 1, column_id, row_item[column_id])
            # 保存文件
            if os.path.exists(file_path):
                os.remove(file_path)
            book.save(file_path)
    
    
    def demo():
        column_heads = ["姓名", "学校", "出生日期", "年龄"]
        row_items = [
            ["张删", "北京大学", "1988-01-02", 29],
            ["王二小", "清华大学", "1989-01-02", 28],
        ]
        file_path = r"d:	1.xls"
        ExcelHelper.create_excel(
            column_heads=column_heads,
            row_items=row_items,
            file_path=file_path
        )
        print("file {0} was created".format(file_path))
    
    
    if __name__ == '__main__':
        demo()

    引入xlwt需要安装openpyxl包

    =========================================================================

  • 相关阅读:
    POJ 2661
    POJ 2643
    POJ 2656
    POJ 2612
    POJ 2636
    搭建WordPress个人博客
    【个人笔记】ximo早期发的脱壳教程——手脱UPX壳
    2.1【欢乐向】攻防世界新手逆向刷题被虐哭日常记录
    吾爱破解培训第一课个人笔记
    第五章 计算机组成
  • 原文地址:https://www.cnblogs.com/TeyGao/p/8799454.html
Copyright © 2020-2023  润新知