zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    一、Django CBV and Django RestFramework
    Web框架及Django初始化
    HTTP协议
    Mysql之存储引擎
    Django之ORM字段相关
    Django之视图
    Django之初步实现登录功能,APP及ORM
    jQuery
    C#基础:飞行棋游戏
    C#基础练习
  • 原文地址:https://www.cnblogs.com/gotesting/p/10076481.html
Copyright © 2011-2022 走看看