如何在python中处理配置文件代码实例
更新时间:2020年09月27日 11:29:18 作者:东何
这篇文章主要介绍了如何在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')
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
PyCharm使用matplotlib报MatplotlibDeprecationWarning问题解决办法
这篇文章主要给大家介绍了关于PyCharm使用matplotlib报MatplotlibDeprecationWarning问题解决的相关资料,主要是 matplotlib版本过高导致的,文中通过图文介绍的非常详细,需要的朋友可以参考下2023-06-06pycharm中导入模块错误时提示Try to run this command from the system ter
这篇文章主要介绍了pycharm中导入模块错误时提示Try to run this command from the system terminal问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-03-03
最新评论