python-利用xlrd模块读取excel数据,将excel数据转换成字典格式
- 前言
- excel测试案例数据
转换成这种格式
- 实现代码
import os
import xlrd
excel_path = '..\data\\test_case.xlsx'
data_path = os.path.join(os.path.dirname(__file__),excel_path)
wb = xlrd.open_workbook(data_path)
ws = wb.sheet_by_name('Sheet1')
all_cases = {}
for r in range(1,ws.nrows):
case_data = {}
for c in range(ws.ncols):
# 获取测试用例标题
tiltle = ws.cell_value(0,c)
data_value = ws.cell_value(r,c)
# 构建测试用例字段标题(k),字段值(v)字典形式
case_data[ws.cell_value(0,c)]=data_value
# 构建测试用例名(k),用例信息(v)字典形式
all_cases[ws.cell_value(r,0)]=case_data
print(all_cases)