Python xlrd的用法

#下载安装xlrd   pip install xlrd==1.2.0
import xlrd
#打开文件,实例化文件对象
excel_obj = xlrd.open_workbook(r"C:\Users\admin\Desktop\xlrd.xlsx")
#实例化sheet对象,下标是从0开始的
sheet_obj = excel_obj.sheet_by_name("Sheet1")  #通过sheet的名字来确定实例化对象
sheet1_obj = excel_obj.sheet_by_index(0)  #通过sheet的下标来确定实例化对象
res = sheet_obj.row_values(0)    #获取第0行的数据
res1 = sheet_obj.row_values(0,start_colx=1,end_colx=3)  #获取第0行的第1,2个的数据,遵循左开右闭
res2 = sheet_obj.col_values(1)  #获取第1列的数据
res3 = sheet_obj.col_values(1,1,3)  #获取第一列,第1,2行数据
res4 = sheet_obj.cell_value(6,1)  #获取第3行第1列的数据
res5 = sheet_obj.nrows   #一共有多少行数据
#将表格数据组装成字典,第一列的url除外
keys = sheet_obj.col_values(0,1) #获取第0列的数据
values = sheet_obj.col_values(1,1) #获取第1列的数据
dict_data = {}
#组装成字典
for i in keys:
    dict_data[i]=values[keys.index(i)]
#或者可以用zip方法,生成元组列表
zip_data= dict(zip(keys,values))
print(zip_data)
print("-----------------")
#打印所有行
for i in range(res5):
    print(sheet_obj.row_values(i))
print("-------------------")
print(res)
print("-------------------")
print(res1)
print("-------------------")
print(res2)
print("-------------------")
print(res3)
print("-------------------")
print(res4)
print("-------------------")
print(res5)

运行结果:

E:\pyProject\pythonpytest\venv\Scripts\python.exe E:/pyProject/pythonpytest/testcase/demo.py
{'platformName': 'Android', 'platformVersion': '6.0.1', 'deviceName': '127.0.0.1:7555', 'appPackage': 'com.zhao.myreader', 'appActivity': 'com.zhao.myreader.ui.home.MainActivity', 'newCommandTimeout': 3600.0}
-----------------
['url', 'http://127.0.0.1:4723/wd/hub', 'a', 'b', 'c']
['platformName', 'Android', '', '', '']
['platformVersion', '6.0.1', '', '', '']
['deviceName', '127.0.0.1:7555', '', '', '']
['appPackage', 'com.zhao.myreader', '', '', '']
['appActivity', 'com.zhao.myreader.ui.home.MainActivity', '', '', '']
['newCommandTimeout', 3600.0, '', '', '']
-------------------
['url', 'http://127.0.0.1:4723/wd/hub', 'a', 'b', 'c']
-------------------
['http://127.0.0.1:4723/wd/hub', 'a']
-------------------
['http://127.0.0.1:4723/wd/hub', 'Android', '6.0.1', '127.0.0.1:7555', 'com.zhao.myreader', 'com.zhao.myreader.ui.home.MainActivity', 3600.0]
-------------------
['Android', '6.0.1']
-------------------
3600.0
-------------------
7

Process finished with exit code 0

excel:

urlhttp://127.0.0.1:4723/wd/hubabc
platformNameAndroid
platformVersion6.0.1
deviceName127.0.0.1:7555
appPackagecom.zhao.myreader
appActivitycom.zhao.myreader.ui.home.MainActivity
newCommandTimeout3600
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值