1. 程式人生 > >python讀取excel,返回dic列表

python讀取excel,返回dic列表

參考 ets blog ref name turn https case 裏的

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列表