網站首頁 編程語言 正文
接著python里面的xlrd模塊詳解(一)中我們我們來舉一個實例:
我們來舉一個從Excel中讀取賬號和密碼的例子并調用:
?1.制作Excel我們要對以上輸入的用戶名和密碼進行參數化,使得這些數據讀取自Excel文件。我們將Excel文件命名為data.xlsx,其中有兩列數據,第一列為username,第二列為password。
?2.讀取Excel代碼如下
#-*- coding:utf-8 -*- import xlrd,time,sys,unittest #導入xlrd等相關模塊 class Data_Excel(unittest.TestCase):# 封裝在Data_Excel類里面方便后面使用 file_addrec = r'C:\Users\liqiang22230\Desktop\date.xlsx' #定義date.xlsx數據維護Excel的路徑文件 def open_excel(self,file = file_addrec):#file = file_addrec #注意在class中def中一定要帶self try:#檢驗文件有沒有被獲取到 self.data =xlrd.open_workbook(file) return self.data except Exception : print(file) print('eero') def excel_table_byindex(self,file = file_addrec,colnameindex=0,by_index='用戶表'): #把這個讀取Excel中封裝在excel_table_byindex函數中,這時需要三個參數1.文件2.sheet名稱,列所在的行數 self.data = xlrd.open_workbook(file)#獲取Excel數據 self.table = self.data.sheet_by_name(by_index)#使用sheet_by_name獲取sheet頁名叫用戶表的sheet對象數據 self.colnames = self.table.row_values(colnameindex)#獲取行數下標為0也就是第一行Excel中第一行的所有的數據值 self.nrows = self.table.nrows #獲得所有的有效行數 list = []#總體思路是把Excel中數據以字典的形式存在字符串中一個字典當成一個列表元素 for rownum in range(1,self.nrows): row = self.table.row_values(rownum)#獲取所有行數每一行的數據值 if row: app = {}#主要以{'name': 'zhangsan', 'password': 12324.0},至于字典中有多少元素主要看有多少列 for i in range(len(self.colnames)): #在這個Excel中,列所在的行有兩個數據,所以沒循環一行就以這兩個數據為鍵,行數的值為鍵的值,保存在一個字典里 app[self.colnames[i]] = row[i] list.append(app) print(list) return list a = Data_Excel() a.excel_table_byindex() if __name__=="__main__": unittest.main()
執行結果如下:
Testing started at 15:47 ... [{'name': 'zhangsan', 'password': 12324.0}, {'name': 'zhangsan', 'password': 12324.0}, {'name': 'lisi', 'password': 923848.0}, {'name': 'lisi', 'password': 923848.0}, {'name': 'wangmazi', 'password': 213123.0}, {'name': 'wangmazi', 'password': 213123.0}] Process finished with exit code 0 Empty test suite.
?3.調用Excel代碼如下:
def Login(self): listdata = excel_table_byindex("E:\\data.xlsx",0)#傳入兩個參數1.文件路徑2.第一行所在下標 if (len(listdata) <= 0 ):#判斷list列表中是否有數據 assert 0 , u"Excel數據異常" for i in range(0 , len(listdata) ):#循環出list中所有的字典 self.driver = webdriver.Chrome() self.driver.get("http://www.effevo.com") assert "effevo" in self.driver.title #點擊登錄按鈕 self.driver.find_element_by_xpath(".//*[@id='home']/div/div[2]/header/nav/div[3]/ul/li[2]/a").click() time.sleep(1) self.driver.find_element_by_id('passname').send_keys(listdata[i]['username'])#切出list下標下標為i的字典鍵為username的值 self.driver.find_element_by_id('password').send_keys(listdata[i]['password'])#切出list下標下標為i的字典鍵為password的值 self.driver.find_element_by_xpath(".//*[@id='content']/div/div[6]/input").click() time.sleep(2) self.driver.close()
總結
原文鏈接:https://www.cnblogs.com/insane-Mr-Li/p/9093212.html
相關推薦
- 2023-05-23 numpy中的掩碼數組的使用_python
- 2023-04-19 Element UI Table常用使用方法(header-cell-style;表頭中的全選框取消
- 2022-10-15 Qt?TCP網絡通信學習_C 語言
- 2022-06-02 jquery實現界面點擊按鈕彈出懸浮框_jquery
- 2022-12-23 Kotlin?try?catch異常處理i詳解_Android
- 2022-11-17 python?泛型函數--singledispatch的使用解讀_python
- 2023-10-09 Linux下查看某個進程占用的CPU、內存
- 2023-10-16 微信小程序radio單選按鈕選中與取消
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支