配置文件是一种计算机文件,可以为一些计算机程序配置参数和初始设置,在内容形式上是一个一个键值对的记录。
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\')
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。