• python读取excel,返回dic列表


    def get_xls_sheets_as_dic(pro_name, xls_name):
        dic_list = []
        xls_path = os.path.join(BASE_PATH, "testFile", 'case', pro_name, xls_name)
        file = open_workbook(xls_path)
        sheets = file.sheets()
    
        for sheet in sheets:
            nrows = sheet.nrows
            ncols = sheet.ncols
            dic_data = {}
            for i in range(1, nrows):
                for j in range(ncols):
                    title = sheet.cell_value(0, j)
                    value = sheet.cell_value(i, j)
                    dic_data[title] = str(value).replace('
    ', '')
                dic_list.append(dic_data)
        return dic_list
    def get_xls_sheet_by_name_as_dic(pro_name, xls_name, sheet_name):
        dic_list = []
        xls_path = os.path.join(BASE_PATH, "testFile", 'case', pro_name, xls_name)
        file = open_workbook(xls_path)
        sheet = file.sheet_by_name(sheet_name)
    
        nrows = sheet.nrows
        ncols = sheet.ncols
        dic_data = {}
        for i in range(1, nrows):
            for j in range(ncols):
                title = sheet.cell_value(0, j)
                value = sheet.cell_value(i, j)
                dic_data[title] = str(value).replace('
    ', '')
            dic_list.append(dic_data)
        return dic_list

    参考:python 将excel里的内容转换为dict

  • 相关阅读:
    前端技术-PS切图
    Html5资料整理
    Html5知识体系
    Html知识体系
    C语言知识结构
    ASP.NET知识结构
    src和href的区别
    Ajax的简单使用
    学习理论
    求模运算法则
  • 原文地址:https://www.cnblogs.com/dannyyao/p/10694096.html
Copyright © 2020-2023  润新知