python中处理配置文件

配置文件是一种计算机文件,可以为一些计算机程序配置参数和初始设置,在内容形式上是一个一个键值对的记录。

python中处理配置文件

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')
原文地址:https://www.cnblogs.com/donghe123/p/13661510.html