PHP头条
热点:

python中如何处理配置文件 python中处理配置文件代码示例


python中如何处理配置文件?本篇文章小编给大家分享一下python中处理配置文件代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

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

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')
  • 上一篇: idea2020.2遇到pom.xml文件报错maven插件tomcat7问题解决方法

  • 下一篇: Maven分模块开发执行指令失败问题解决方法

www.phpzy.comtrue/php/40111.htmlTechArticlepython中如何处理配置文件 python中处理配置文件代码示例 python中如何处理配置文件?本篇文章小编给大家分享一下python中处理配置文件代码示例,文章代码介绍的很详细,小编觉得挺不...

相关文章

    暂无相关文章

PHP之友评论

今天推荐