python - 读取配置文件

# -*- coding:utf-8 -*-

'''
@project: jiaxy
@author: Jimmy
@file: read_config.py
@ide: PyCharm Community Edition
@time: 2018-12-06 13:53
@blog: https://www.cnblogs.com/gotesting/

'''


'''

配置文件

    .ini   .properties  .conf 等常见配置文件

1. section  :片段
2. option   :选项

    存在配置文件中的数据,读取出来后,全都是字符串类型。


'''

import configparser

# 创建一个对象,用来读取配置文件
cf = configparser.ConfigParser()
# 读取配置文件
cf.read('case.conf')

print(cf.sections())
print(cf.options('Student'))

# section和option的组合,可以确定一个想要的值
# 获取指定值的方法, get 或者 [][]
print(cf.get('Student','score'))
print(cf['Student']['score'])

print(type(cf.get('Student','score')))



class ReadConfig:

    def read_config(self,filename,section,option):
        cf = configparser.ConfigParser()
        cf.read(filename)
        value = cf.get(section,option)
        return value

if __name__ == '__main__':

    config = ReadConfig().read_config('case.conf','Student','score')
    print(config)

 

 

posted @ 2018-12-06 14:17  JiaxyGogogo  阅读(480)  评论(0编辑  收藏  举报