编程语言
首页 > 编程语言> > python读取excel,返回dic列表

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('\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

标签:sheet,name,ncols,python,excel,value,dic,xls
来源: https://www.cnblogs.com/dannyyao/p/10694096.html