首页 > 编程语言 > 详细

python读取excel,返回dic列表

时间:2019-04-12 10:12:56      阅读:402      评论:0      收藏:0      [点我收藏+]
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(\n, ‘‘)
            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(\n, ‘‘)
        dic_list.append(dic_data)
    return dic_list

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

python读取excel,返回dic列表

原文:https://www.cnblogs.com/dannyyao/p/10694096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!