Python读取Excel

Python使用第三方库xlrd读取Excel

接口自动化测试中,数据和代码分离才能逻辑更清晰,所以用excel管理用例数据比较方便

一、安装xlrd

pip install xlrd

二、使用命令

#coding=utf-8
import xlrd

wb=xlrd.open_workbook("test_user_data.xls") #打开excel
sh=wb.sheet_by_name("TestUserLogin") #按表格名称获取表格
print(sh.nrows) #获取有效的数据行数
print(sh.ncols) #获取有效的数据列数
print(sh.cell(0,0).value) #获取单元格值
print(sh.row_values(0)) #获取第一行数据
print(dict(zip(sh.row_values(0),sh.row_values(1)))) #将表头和数据整理成字典

for i in range(1,sh.nrows):
zipdata=dict(zip(sh.row_values(0),sh.row_values(i)))
print zipdata
原文地址:https://www.cnblogs.com/testerlina/p/12942730.html