import xlrd from collections import defaultdict def read(file, sheet_index=0): """ :param file: 文件路径 :param sheet_index: 读取的工作表索引 :return: 二维数组 """ workbook = xlrd.open_workbook(file) # all_sheets_list = workbook.sheet_names() # print("本文件中所有的工作表名称:", all_sheets_list) # 按索引读取工作表 print(len(workbook.sheets())) sheet = workbook.sheet_by_index(sheet_index) print("工作表名称:", sheet.name) print("行数:", sheet.nrows) print("列数:", sheet.ncols) # 按工作表名称读取数据 # second_sheet = workbook.sheet_by_name("b") # print("Second sheet Rows:", second_sheet.nrows) # print("Second sheet Cols:", second_sheet.ncols) # 获取单元格的数据 # cell_value = sheet.cell(1, 0).value # print("获取第2行第1列的单元格数据:", cell_value) data = [] for i in range(0, sheet.nrows): data.append(str(sheet.cell(i,1))) d = defaultdict(list) b = {}.fromkeys(data).keys() for j in b: temp = [] for k in range(0,len(data)): if j == data[k]: temp.append(str(sheet.cell(k,6))) d[j] = temp print(d) # for k, va in [(v, i) for i, v in enumerate(data)]: # d[k].append(va) # print(d) return data if __name__ == '__main__': read('D://1.xlsx')
python过滤数据
最新推荐文章于 2024-03-06 16:11:40 发布