do_excel 3种 根据需要使用

import pandas as pd
from openpyxl import load_workbook

class DoExcel:
#第一种  读取所有sheet
    # def get_data(self,filename):
    #     wb =load_workbook(filename)
    #     mode = eval(ReadConfig.get_config(case_config_path,"MODE","mode"))
    #     # tel = getattr(GetData,"NoRegTel") #利用反射从excel里的init表中拿到的数据
    #     test_data =[]
    #     for key in mode:#遍历这个存在配置文件里面的字典
    #         print("这是表名:",mode[key],type(mode[key]))
    #         sheet = wb[key] #表单名
    #         if mode[key] == 'all':
    #             for i in range(2, sheet.max_row + 1):
    #                 row_data = {}  # 字典
    #                 row_data["case_id"] = sheet.cell(i, 1).value
    #                 row_data["username"] = sheet.cell(i, 2).value
    #                 row_data["password"] = sheet.cell(i,3).value
    #                 row_data["email"] = sheet.cell(i,4).value
    #                 row_data["sheet_name"] = key
    #                 test_data.append(row_data)
    #
    #         else:
    #             for case_id in mode[key]:#不为all就为列表
    #                 row_data = {}  # 字典
    #                 row_data["case_id"] = sheet.cell(case_id+1, 1).value
    #                 row_data["username"] = sheet.cell(case_id+1, 2).value
    #                 row_data["password"] = sheet.cell(case_id+1, 3).value
    #                 row_data["email"] = sheet.cell(i, 4).value
    #                 row_data["sheet_name"] = key
    #                 test_data.append(row_data)
    #
    #     return test_data
    #
    #
    # def write_back(self,filename,sheetname,i,result):#回写数据
    #     wb = load_workbook(filename)
    #     sheet = wb[sheetname]
    #     sheet.cell(i+1,5).value = result
    #     wb.save(filename)#保存结果


#第二种   pandas读取一张sheet
    def get_data(self,filename,sheetname):
        df = pd.read_excel(filename,sheet_name=sheetname)
        test_data=[]
        for i in df.index.values:
            row_data = df.loc[i].to_dict()
            test_data.append(row_data)
        return test_data


#第三种   pandas 读取所有sheet
    # def get_data(self,filename):
    #     mode = eval(ReadConfig().get_config(case_config_path,"MODE","mode"))
    #
    #     test_data = []
    #     for key in mode:
    #
    #         df = pd.read_excel(test_case_path,sheet_name=key)
    #         if mode[key] == 'all':
    #             for i in df.index.values:
    #                 row_data = df.loc[i,"case_id":"expect"].to_dict()
    #                 test_data.append(row_data)
    #
    #         else:
    #             for case_id in mode[key]:
    #                 row_data = df.loc[case_id,"case_id":"expect"].to_dict()
    #                 test_data.append(row_data)
    #     return test_data


if __name__ == '__main__':
    do_excel = DoExcel()
    test_data = do_excel.get_data(test_case_path)
    print(len(test_data))
    print(test_data)
    #

猜你喜欢

转载自www.cnblogs.com/lexus168/p/12766905.html