Selenium2+python automation 58-read Excel data (xlrd)

foreword

When there are multiple login accounts, we generally use excel to store test data. In this lesson, python reads the excel method and saves it in dictionary format.

 

1. Environmental preparation

1. Install the xlrd module first, open cmd, and enter pip install xlrd to install online

>>pip install xlrd

 

2. Basic operation

1. The basic operation method of exlce is as follows

# Open the exlce table, the parameter is the file path
data = xlrd.open_workbook('test.xlsx')

# table = data.sheets()[0] # Get by index order
# table = data.sheet_by_index(0) # Get
table by index order = data.sheet_by_name(u'Sheet1') # Get by name

nrows = table.nrows # Get the total number of rows
ncols = table.ncols # Get the total number of columns

# Get the value of a row or column, the parameter is the number of rows
print table.row_values(0) # Get the value of the first row
print table.col_values(0) # Get the value of the first column

 

Three, excel storage data

1. Store data in excel, the first row is the title, which is the key value in the corresponding dictionary, such as: username, password

2. If there are pure numbers in the excel data, you must right-click "set cell format" text format, otherwise the read data is a floating point number

(Set the cell format first and then edit it. If the editing is successful, there is a small triangle icon in the upper left corner)

 

Fourth, the package reading method

1. The final data read is the list type data of multiple dictionaries. The first row of data is the key value in the dictionary, and the second row corresponds to the value value one by one.

2. The packaged code is as follows

# coding:utf-8
import xlrd
class ExcelUtil():
    def __init__(self, excelPath, sheetName):
        self.data = xlrd.open_workbook(excelPath)
        self.table = self.data.sheet_by_name(sheetName)
        # Get the first row As the key value
        self.keys = self.table.row_values(0)
        # Get the total number of
        rows self.rowNum = self.table.nrows
        # Get the total number of columns
        self.colNum = self.table.ncols

    def dict_data(self):
        if self .rowNum <= 1:
            print("The total number of rows is less than 1")
        else:
            r = []
            j=1
            for i in range(self.rowNum-1):
                s = {}
                # Take the corresponding values ​​from the second row
                values = self.table.row_values(j)
                for x in range(self.colNum):
                    s[self.keys[x]] = values[x]
                r.append(s)
                j+=1
            return r

if __name__ == "__main__":
    filepath = "D:\\test\\web-project\\5ke\\testdata.xlsx"
    sheetName = "Sheet1"
    data = ExcelUtil(filepath, sheetName)
    print data.dict_data()

operation result:

[{u'username': u'python\u7fa4', u'password': u'226296743'},

{u'username': u'selenium\u7fa4', u'password': u'232607095'},

{u'username': u'appium\u7fa4', u'password': u'512200893'}]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339464&siteId=291194637