python配置文件操作

步骤:

1.导入模块  import configparser

2.创建实例 cf = configparser.ConfigParser()

3.读取配置文件,若配置文件中有中文,则需设置编码格式 cf.read(filename, encoding="utf-8")

4.读取配置文件中对应的section  cf[section]

5.读取配置文件中section中的option cf[section][option]

例:

import configparser


class ReadConfig:

    @staticmethod
    def read_config():      
        cf = configparser.ConfigParser()
        cf.read("myconf.conf", encoding="utf-8")
        value = cf["CASE"]["button"]
        print value

ReadConfig.read_config()

配置文件myconf.conf中内容:

[CASE]      # section
button=all   # option

运行结果:

button=all

 

posted @ 2018-12-13 14:25  玖兰枢_优姬  阅读(270)  评论(0编辑  收藏  举报