python+selenium 从excel中读取数据并转化为字典

import selenium
import xlrd
import xlwt
class ExcelRead():
    def __init__(self):
        #获取excel文件的路径,先前卡在这个地方很久,是因为网络上说excel格式是xls,这里换称xlsx就好了
        excelpath = r'D:/pythonlearngit/YYSTesting/resource/excel.xlsx'
        self.data = xlrd.open_workbook(excelpath)
        self.dataRowDict = []

    def getExcelAllData(self,sheetName):
        #通过索引定位到取excel的哪个sheet   也可以用id来获取
        table = self.data.sheet_by_name(sheetName)
        #获取总行数
        totalRows = table.nrows
        #保存从excel表中读取的值,每一行是一个list,allData中这个sheet的保存了所有行
        dataAll = []
        dataAllDict = []
        for i in range(0,totalRows):
            dataAll.append(table.row_values(i))
        #将list转换为字典dict   zip的意思是将前面一个数组的值,与后面一个数组的值一一对应起来
        for i in range(1,len(dataAll)):
            zipped = zip(dataAll[0],dataAll[i])
            dataAllDict.append(dict(zipped))
        return dataAllDict

    def getExcelRowData(self,sheetName,rowNum):
        #取特定行的list数组
        dataAllDict = self.getExcelAllData(sheetName)
        self.dataRowDict = dataAllDict[rowNum-1]
        Log.getInfo("打印%s行数据"%(rowNum-1))
        Log.getInfo(self.dataRowDict)

    def getBuyerLoginName(self):
        dataRowDict = self.dataRowDict
        loginName = dataRowDict['userName']
        return loginName

你可能感兴趣的:(python+selenium 从excel中读取数据并转化为字典)