讀取適用於pytest的參數化數據

import xlrd

def open_file(filename,tablename):
    '''打開文件'''
    open_file = xlrd.open_workbook(filename) #打開表
    open_table = open_file.sheet_by_name(tablename) #打開Tab
    nrows = open_table.nrows  # 獲取總行數
    ncols = open_table.ncols  # 獲取總列數
    keys =open_table.row_values(0) #獲取第一行作爲Key
    ncols_ed =open_table.col_values(0)[1:] #獲取第一列,作爲ids值,供參數化時使用
    # print(ncols_ed)
    return open_table,nrows,ncols,keys,ncols_ed


def read_excel(open_table,nrows,ncols,keys,ncols_ed):
    '''讀取excel內容'''

    j=1
    list1=[]
    for i in range(nrows-1):
        dict1 = {}
        values= open_table.row_values(j) #取一列進行遍歷,將key和value對應
        for k in range(ncols):
            dict1[keys[k]]=values[k]
        j +=1
        list1.append(tuple(dict1.values())) #取字典的值,加入列表之前轉換成元祖
    # 返回list1,ncols_ed組成的元祖
    return list1,ncols_ed


filename="/Users/liujing/PycharmProjects/untitled1/practice/test_datas/creat_special_subjest_dates.xlsx"
tablename="Sheet1"
a = open_file(filename,tablename)
# print(read_excel(*a))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章