python excel和yaml文件的讀取封裝
更新時間:2021年01月12日 10:48:17 作者:**綿綿羊**
這篇文章主要介紹了python excel和yaml文件的讀取封裝,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
excel
import os import xlrd PATH = lambda p: os.path.abspath( os.path.join(os.path.dirname(__file__), p) ) class ExcelData: def __init__(self, file, sheet="sheet1", title=True): # 判斷文件存在不存在 if os.path.isfile(PATH(file)): self.file = PATH(file) self.sheet = sheet self.title = title self.data = list() self.workbook = xlrd.open_workbook(self.file) else: raise FileNotFoundError("文件不存在") @property def get_data(self): """獲取表格數(shù)據(jù)""" if not self.data: # 判斷表單名稱 if type(self.sheet) not in [int, str]: raise Exception("表單名稱類型錯誤") else: if type(self.sheet) == int: book = self.workbook.sheet_by_index(self.sheet) else: book = self.workbook.sheet_by_name(self.sheet) # 判斷表格是否有表頭,有則輸出列表嵌套字典形式數(shù)據(jù),否則輸入列表嵌套列表形式數(shù)據(jù) if self.title: title = book.row_values(0) for i in range(1, book.nrows): self.data.append(dict(zip(title, book.row_values(i)))) # 可參考字典章節(jié) else: for i in range(book.nrows): self.data.append(book.row_values(i)) return self.data @property def get_sheets(self): """獲取所有表單,這個在后續(xù)會用到""" book = self.workbook.sheets() return book
調(diào)用操作
infos = ExcelData("htmls/測試用例.xlsx", "登入頁面", True).get_data print(infos) sheets = ExcelData("htmls/測試用例.xlsx").get_sheets print(sheets)
yaml
import os import yaml from yamlinclude import YamlIncludeConstructor YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader) # 用于yaml文件嵌套 PATH = lambda p: os.path.abspath(os.path.join( os.path.dirname(__file__), p )) class YamlData: def __init__(self, file): if os.path.isfile(PATH(file)): self.file = PATH(file) else: raise FileNotFoundError("文件不存在") @property # 設(shè)置屬性,調(diào)用data方法時可通過調(diào)用屬性,不需要帶括號 def data(self): with open(file=self.file, mode="rb") as f: infos = yaml.load(f, Loader=yaml.FullLoader) # infos = yaml.load(f) return infos
調(diào)用操作
infos = YamlData("htmls/loginsucess.yaml").data print(infos) "D:\Program Files\Python\Python37-32\python.exe" D:/demo/yamldata.py {'id': 'login_001', 'module': '登入頁面', 'title': '登入時賬號為空', 'message': '已打開鏈接', 'testcase': [{'element_info': 'css->[placeholder="請輸入賬號"]', 'operate_type': 'send_keys', 'keys': 'SSSS', 'info': '點擊賬號輸入框,輸入賬號'}, {'element_info': 'css->[placeholder="請輸入密碼"]', 'operate_type': 'send_keys', 'keys': 'XXX', 'info': '點擊密碼輸入框,輸入密碼'}, {'element_info': 'div->"登 錄"', 'operate_type': 'click', 'info': '點擊登入菜單'}, {'operate_type': 'is_sleep', 'keys': 3, 'info': '等待進入'}], 'check': None} Process finished with exit code 0
以上就是python excel和yaml文件的讀取與封裝的詳細內(nèi)容,更多關(guān)于python 文件讀取與封裝的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python回溯法實現(xiàn)數(shù)組全排列輸出實例分析
這篇文章主要介紹了python回溯法實現(xiàn)數(shù)組全排列輸出,以實例形式較為詳細的分析了全排列的定義及回溯法的實現(xiàn)技巧,需要的朋友可以參考下2015-03-03基于Python制作一個端午節(jié)相關(guān)的小游戲
端午節(jié)快樂,今天我將為大家?guī)硪黄嘘P(guān)端午節(jié)的編程文章,希望能夠為大家獻上一份小小的驚喜,我們將會使用Python來實現(xiàn)一個與端午粽子相關(guān)的小應(yīng)用程序,在本文中,我將會介紹如何用Python代碼制做一個“粽子拆解器”,感興趣的小伙伴歡迎閱讀2023-06-06Python numpy數(shù)組轉(zhuǎn)置與軸變換
這篇文章主要介紹了Python numpy數(shù)組轉(zhuǎn)置與軸變換,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11基于python3監(jiān)控服務(wù)器狀態(tài)進行郵件報警
這篇文章主要介紹了基于python3監(jiān)控服務(wù)器狀態(tài)進行郵件報警,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10Python 中將秒轉(zhuǎn)換為小時、分鐘和秒的示例代碼
這篇文章主要介紹了在 Python 中將秒轉(zhuǎn)換為小時、分鐘和秒,本篇文章將討論使用 Python 中的四種不同方法來使用、管理秒并將其轉(zhuǎn)換為天、小時、分鐘和秒,需要的朋友可以參考下2023-05-05