如何在python中處理配置檔案程式碼例項
阿新 • • 發佈:2020-09-28
配置檔案是一種計算機檔案,可以為一些計算機程式配置引數和初始設定,在內容形式上是一個一個鍵值對的記錄。
testcase.yaml檔案:
excel:
filename: "testcase.xlsx"
將yaml庫做二次封裝:
import yaml class HandleYaml: def __init__(self,filename=None): if filename is None: self.filename = 'testcase.yaml' else: self.filename = filename with open(filename,encoding="utf-8") as file: # 用上下文管理器開啟yaml配置檔案 self.data = yaml.full_load(file) # 載入yaml檔案,返回一個巢狀字典的字典 def get_data(self,section,option): return self.data[section][option] if __name__ == "__main__": s = HandleYaml() s.get_data('excel','filename')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。