002 读取文件
# 读取文件 class ExcelUtil(): def __init__(self, excel_path, sheet_name="int_lp"): self.data = xlrd.open_workbook(excel_path) self.table = self.data.sheet_by_name(sheet_name) # 设置key值 : 获取第一行 self.keys = self.table.row_values(0) # 获取总行数 self.row_count = self.table.nrows # 获取总列数 self.line_count = self.table.ncols def get_row_count(self): return self.row_count def dict_data(self): if self.row_count <= 1: print("Excel Sheet表总行数少于1") else: data = [] # 返回数据 : 列表 + 字典 loop = 1 while(loop < self.row_count): # 循环行数 dict_row = {} values = self.table.row_values(loop) # 循环列数 # print(values) for line in range(self.line_count): # 类型转换 line_num = ["star_level", "lp_count"] if self.keys[line] in line_num: dict_row[self.keys[line]] = int(values[line]) else: dict_row[self.keys[line]] = values[line] data.append(dict_row) loop = loop + 1 return data