全文检索关键字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | # -*- coding: utf-8 -*- """ 1、获取excel表中api接口名称列表 2、在封装的接口库中比对搜索关键字 3、接口名存在记为1,否则为0,写入excel表中 """ import xlwt, xlrd import io # 记录列数,全局变量,还原方便 columnIndex = 0 # 记录行数 rowIndex = 1 # 记录任务个数 taskCount = 0 # 读excel def ReadExcel(filepath): xlsx = xlrd.open_workbook(filepath) # 打开文件 print ( 'All sheets: %s' % xlsx.sheet_names()) # 查看所有sheet列表 sheet1 = xlsx.sheets()[ 1 ] # 获得第1张sheet,索引从0开始 sheet1_name = sheet1.name # 获得名称 sheet1_cols = sheet1.ncols # 获得列数 sheet1_nrows = sheet1.nrows # 获得行数 print ( 'Sheet1 Name: %s\nSheet1 cols: %s\nSheet1 rows: %s' % (sheet1_name, sheet1_cols, sheet1_nrows)) print ( '-----------------接口列表----------------' ) api_name = [] for i in range ( 1 , sheet1_nrows): # 逐行打印sheet1数据 print (sheet1.row_values(i)) # 打印全文 if sheet1.row(i)[ 1 ].value ! = '': # 读取2列 api_name.append(sheet1.row(i)[ 1 ].value.encode( 'UTF-8' )) # 读取的excel值是unicode字符集转uft8编码 #api_name.append(sheet1.row(i)[1].value) print (api_name) return api_name # 直接读取文件内容检索,适合一般大小文件 def content_search1(filepath, keyword): with open (filepath, 'r' ) as f: content = f.read() #print(content) if keyword in content: return True return False # 按行检索关键字,适合大文件 def content_search(filepath, keyword): f = open (filepath, 'r' ) for line in f: if keyword in line: f.close() return True f.close() return False if __name__ = = '__main__' : # 读取api接口名 filepath = 'Hub接口用例情况统计.xlsx' api_name_list = ReadExcel(filepath) # 用例地址 file_path = 'iot_hub.py' # 使用xlwt模块 wb = xlwt.Workbook(encoding = 'utf-8' ) ws = wb.add_sheet( '统计' , cell_overwrite_ok = True ) title_list = [ '接口名' , '是否封装' ] # 用例标题写入excel首行 for j in range ( 0 , len (title_list)): ws.write( 0 , j, title_list[j]) j + = 1 r_true = 0 r_false = 0 for i in range ( 0 , len (api_name_list)): api_name = api_name_list[i] # 检索的keyword flag = content_search(file_path, api_name_list[i]) # 是否存在 if flag: r_true + = 1 else : r_false + = 1 taskCount + = 1 # 任务个数 ws.write(rowIndex, columnIndex, api_name) # 写任务id ws.write(rowIndex, columnIndex + 1 , flag) # 写任务name i + = 1 rowIndex + = 1 print ( '任务总数%s:' % taskCount) print ( 'True %d False %d' , r_true, r_false) # 保存Excel文档 wb.save( '统计表.xls' ) |
本文来自博客园,作者:ReluStarry,转载请注明原文链接:https://www.cnblogs.com/relustarry/p/16110972.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统