In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of Excel data maintenance in python selenium. I hope you will get something after reading this article. Let's discuss it together.
Let's give an example of reading an account and password from Excel and calling:
1. To make Excel, we need to parameterize the user name and password entered above so that the data can be read from the Excel file. We name the Excel file data.xlsx, which has two columns of data, the first column username and the second column password.
two。 Read the Excel code as follows
#-*-coding:utf-8-*-import xlrd,time,sys,unittest # Import xlrd and other related modules class Data_Excel (unittest.TestCase): # encapsulated in the Data_Excel class to facilitate the subsequent use of file_addrec = ritual C:\ Users\ liqiang22230\ Desktop\ date.xlsx' # define the date.xlsx data maintenance Excel path file def open_excel (self File = file_addrec): # file = file_addrec # Note: in class, def must take self try:# to check whether the file has been obtained 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=' user table'): # encapsulate this read Excel in the excel_table_byindex function At this time, three parameters 1. File 2.sheet name Number of rows in the column self.data = xlrd.open_workbook (file) # get Excel data self.table = self.data.sheet_by_name (by_index) # use sheet_by_name to get the sheet object data of the sheet page called user table self.colnames = self.table.row_values (colnameindex) # get the number of rows with an index of 0, that is, all of the first row in the first row Excel The data value of self.nrows = self.table.nrows # to get all the valid rows list = [] # the general idea is to treat the data in Excel as a dictionary in the string as a list element for rownum in range (1 Self.nrows): row = self.table.row_values (rownum) # get the data value of all rows and each row if row: app = {} # mainly {'name':' zhangsan', 'password': 12324.0} As for the number of elements in the dictionary, it mainly depends on the number of columns for i in range (len (self.colnames)): # in this Excel, the row of the column has two data, so it takes these two data as keys without looping a row, and the value of the number of rows is the value of the key Save it in a dictionary app[ self.colnamesename] = row [I] list.append (app) print (list) return lista = Data_Excel () a.excel_table_byindex () if _ _ name__== "_ _ main__": unittest.main ()
The implementation results are as follows:
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 0Empty test suite.
3. The code to call Excel is as follows:
Def Login (self): listdata = excel_table_byindex ("E:\\ data.xlsx", 0) # pass in two parameters 1. File path 2. The subscript if of the first line (len (listdata))
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.