python excel和yaml文件的讀取封裝
更新時(shí)間: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("表單名稱類型錯(cuò)誤")
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):
"""獲取所有表單,這個(gè)在后續(xù)會(huì)用到"""
book = self.workbook.sheets()
return book
調(diào)用操作
infos = ExcelData("htmls/測(cè)試用例.xlsx", "登入頁(yè)面", True).get_data
print(infos)
sheets = ExcelData("htmls/測(cè)試用例.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方法時(shí)可通過(guò)調(diào)用屬性,不需要帶括號(hà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': '登入頁(yè)面', 'title': '登入時(shí)賬號(hào)為空', 'message': '已打開鏈接', 'testcase': [{'element_info': 'css->[placeholder="請(qǐng)輸入賬號(hào)"]', 'operate_type': 'send_keys', 'keys': 'SSSS', 'info': '點(diǎn)擊賬號(hào)輸入框,輸入賬號(hào)'}, {'element_info': 'css->[placeholder="請(qǐng)輸入密碼"]', 'operate_type': 'send_keys', 'keys': 'XXX', 'info': '點(diǎn)擊密碼輸入框,輸入密碼'}, {'element_info': 'div->"登 錄"', 'operate_type': 'click', 'info': '點(diǎn)擊登入菜單'}, {'operate_type': 'is_sleep', 'keys': 3, 'info': '等待進(jìn)入'}], 'check': None}
Process finished with exit code 0
以上就是python excel和yaml文件的讀取與封裝的詳細(xì)內(nèi)容,更多關(guān)于python 文件讀取與封裝的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Flask與FastAPI對(duì)比選擇最佳Python?Web框架的超詳細(xì)指南
Flask和FastAPI都是流行的Python?Web框架,各有特點(diǎn),Flask輕量級(jí)、靈活,適合小型項(xiàng)目和原型開發(fā)但不支持異步操作,FastAPI高性能、支持異步,內(nèi)置數(shù)據(jù)驗(yàn)證和自動(dòng)生成API文檔,適合高并發(fā)和API開發(fā),需要的朋友可以參考下2025-02-02
Pygame實(shí)現(xiàn)監(jiān)聽鼠標(biāo)示例詳解
這篇文章主要介紹了通過(guò)Pygame模塊實(shí)現(xiàn)監(jiān)聽鼠標(biāo)的功能,文章的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的價(jià)值,感興趣的小伙伴可以了解一下2021-12-12
Ubuntu下升級(jí) python3.7.1流程備忘(推薦)
這篇文章主要介紹了Ubuntu下升級(jí) python3.7.1流程備忘,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
python定時(shí)任務(wù)sched庫(kù)用法簡(jiǎn)單實(shí)例
sched可用于定時(shí)任務(wù),唯一需要注意的就是,這些任務(wù)在一個(gè)線程中運(yùn)行,如果前面的任務(wù)耗時(shí)過(guò)長(zhǎng),則后面的任務(wù)將順延執(zhí)行,下面這篇文章主要給大家介紹了關(guān)于python定時(shí)任務(wù)sched庫(kù)用法的相關(guān)資料,需要的朋友可以參考下2023-01-01
簡(jiǎn)單談?wù)凱ython中的元祖(Tuple)和字典(Dict)
這篇文章主要介紹了關(guān)于Python中元祖(Tuple)和字典(Dict)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04

