接口自动化-- do——excel pandas 改造

from tools.read_config import ReadConfig
from tools import project_path
from tools.get_data import GetData
import pandas as pd

class DoExcel:

    def get_data(self,file_name):
        df = pd.ExcelFile(file_name)   #8,9 是获取表格里的sheetname,结果为一个列表
        sheets = df.sheet_names
        for sheet in sheets:    #将sheet名循环,读取每一个sheet
            df = pd.read_excel(file_name,sheet_name=sheet)
            mode = eval(ReadConfig().get_config(project_path.case_config_path,"MODE","mode"))
            test_data=[]
            for key in mode:
                # sheet = wb[key]
                if mode[sheet] =='all':
                    for i in df.index.values:    #df.index.values为sheet的行号列表
                        row_data = df.loc[i].to_dict()   #循环读取每行的数据并转化为字典
                        test_data.append(row_data)      #将字典放入列表
                else:
                    for case_id in mode[sheet]:
                        row_data=df.loc[case_id].to_dict()
                        test_data.append(row_data)
            return test_data


if __name__ == '__main__':
    do_excel = DoExcel()
    test_data = do_excel.get_data(r"D:\BaiduNetdiskDownload\NMB_API_61\test_data\test_data.xlsx")
    print(test_data)

参考 :https://www.jb51.net/article/166820.htm

   https://www.cnblogs.com/liulinghua90/p/9935642.html

  https://blog.csdn.net/u010801439/article/details/80052677

猜你喜欢

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