python读取execl数据文件

import xlrd
#将excel中数据转换为列表
def excel_to_list(file,tag):
data_list=[]
#解析文件
book=xlrd.open_workbook(file)
# print(type(book))
#获取到自己想要的标签页
tag=book.sheet_by_name(tag)
#获取tag页行数
row_num=tag.nrows
#获取tag页列数
# cols_num=tag.ncols
# print(row_num,cols_num)
#获取表头,及第一行行数据
header=tag.row_values(0)
#print(header)
#从表头下一行开始读
for i in range(1,row_num):
#读书没一行数据
row_data=tag.row_values(i)
#等长两列表转为字典,zip函数等长处理, 表头为key,用例中数据为value。
d=dict(zip(header,row_data))
# print(d)
data_list.append(d)
return data_list

#在数据列表中根据测试用例名称获取对应的用例字典
def get_test_data(test_name,test_list):
for test_dic in test_list:
if test_name==test_dic["test_name"]:
return test_dic
if __name__ == '__main__':
file="接口测试用例.xls"
tag='BingdingCard'
test_name="test_no_exist_cardnumber_bound"
test_list=excel_to_list(file,tag)
print(get_test_data(test_name,test_list))

猜你喜欢

转载自www.cnblogs.com/zzzao/p/9180632.html
今日推荐