数据字典实例考试系统(python 操作execle 进行数据字典添加列表)

import openpyxl import os import sys def get_max_row(sheet): i=sheet.max_row real_max_row = 0 while i > 0: row_dict = {i.value for i in sheet[i]} if row_dict == {None}: i = i-1 else: real_max_row = i break return real_max_row info={} def excel_config_info_function(): parent = os.path.dirname(os.path.realpath(sys.argv[0])) #parent = os.path.dirname(os.path.realpath(__file__))--打包成exe 会路径不对 print("获取其父目录——" + parent) # 从当前文件路径中获取目录 filenames_0 = os.listdir(parent+''+'begin_config') for f_name_0 in filenames_0: if f_name_0: wookbook = openpyxl.load_workbook(parent+''+'begin_config' + '' + f_name_0) book_sheet=wookbook['Sheet1'] #清空excle配置表数据 max_row_v=get_max_row(book_sheet) for x in range(1,max_row_v+1): list=[] for y in range(2, book_sheet.max_column + 1): #去掉excel空的选项 用这个dict=={None} 过滤空字符串 excel中空字符串是None dict = {book_sheet.cell(x, y).value} if dict=={None}: continue else: list.append(str(book_sheet.cell(x, y).value).strip()) info[str(book_sheet.cell(x, 1).value).strip()]=list print(info) print('=========ok==配置文件info执行完成========') return info if __name__ == '__main__': excel_config_info_function()


输出结果


数据字典实例

您可以还会对下面的文章感兴趣

最新评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

使用微信扫描二维码后

点击右上角发送给好友