1.将exeel中的数据读取成字典格式,key为字段,value为cell的值,如下所示,可以得到一个列表,列表里都是字典
def readexcel(self,file):
wb=openpyxl.load_workbook(file)
sheets=wb.sheetnames
cases = []
case_data = []
titles1 = []
titles2=[]
# print(sheets)
#根据下标获取sheet名称,sheet名称是检测的语言
for i in range(len(sheets)):
sheet=wb.sheetnames[i]
sheet=wb[sheet]
cols=sheet.max_column
rows=sheet.max_row
rows1=list(sheet.rows)
#遍历所有行,
#从每个sheet表格中获取用例长度,得到表头
for i in range(rows-1):
for key in rows1[0]:
titles1.append(key.value)
for row in rows1[1:]:
for cel in row:
case_data.append((cel.value))
return cases
2.调用上面的方法,根据判断的条件进行筛选查找,
1 def readexcle_inner(self,file,language,typename): 2 3 rawdata=self.readexcel(file) 4 data=[] 5 6 for i in (rawdata): 7 if i[language]==language and i[typename]=='yes': 8 data.append(i['DetailClassChin']) 9 print(data)