1. 程式人生 > 其它 >pandas讀取Excel測試資料

pandas讀取Excel測試資料

import pandas as pd
import os


class ExcelUnit:

    def read_path(self):
        # dirname獲取檔案當前目錄路徑 字串分割獲取到專案的路徑 abspath設定絕對路徑
        return os.path.abspath((os.path.dirname(__file__)).split("common")[0])

    def read_excel(self, sheet_name):
        # 讀取測試資料檔案 通過傳參的方式設定每次需要讀取的工作簿
        df = pd.read_excel(self.read_path() + r"
\testData\test.xlsx", sheet_name=sheet_name) # 獲取總行總列數,通過索引的方式獲取總行 sum_row = df.shape[0] # 建立一個儲存每行資料的列表 sava_list = [] # 遍歷每一行資料 for lst in range(sum_row): # 建立一個臨時儲存資料的列表 temp_list = [] # 遍歷每列的資料 for vls in
df.loc[lst].values: # 將遍歷的資料新增到列表中 temp_list.append(vls) # 將每次行的資料加入到列表中 sava_list.append(temp_list) return sava_list