zoukankan      html  css  js  c++  java
  • configparser模块

    configparser模块

    echo   $@ $# $? $*

     具体代码示例代码

    import ConfigParser
    import os
    
    
    class Config(object):
        def __init__(self, config_filename="cgss.conf"):
            print(config_filename)
            file_path = file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), config_filename)  #注意这句的路径问题
            self.cf = ConfigParser.ConfigParser()
            self.cf.read(file_path)
            print(self.get_sections())
    
        def get_sections(self):
            return self.cf.sections()
    
        def get_options(self, section):
            return self.cf.options(section)
    
        def get_content(self, section):
            result = {}
            for option in self.get_options(section):
                value = self.cf.get(section, option)
                result[option] = int(value) if value.isdigit() else value
            return result
    
    ret = Config().get_content("mongo")
    print ret

    cgss.cnf   
    [notdbMysql] host
    = 192.168.1.101 port = 3306 user = root password = python123

    详解

    configparse用于处理特定格式的文件,其本质上利用open来操作文件(比如配置文件)
    **********配置文件***************
    #注释1这个一个配置文件

        [secton1] #节点  
        k1 = v1 #
        k2:v2  #
        [section2] #节点  
        k1 = v2#

    @1)、获取所有节点

    import configparser  
    config = configparser.ConfigParser()  
    config.read('xxooo.txt', encoding='utf-8')  
    ret = config.sections()  
    print(ret)  

    @2)、获取指定节点下所有的键值对

        import configparser  
        config = configparser.ConfigParse()  
        config.read('xxoo.txt', encoding='utf-8')  
        ret = config.items('sections')  
        print(ret)  

    @3)、获取指定节点下所有的键

        import configparser  
        config = configparser.ConfigParser()  
        config.read("xxoo.txt", encoding="utf-8")  
        ret = config.options('section1')  
        print(ret)  

    @4)、获取指定节点下指定key值

        import configparser  
        config = configparser.ConfigParser()  
        config.read('xxoo.txt', encoding='utf-8')  
        v = config.get('section1', 'k1')  
        #v = config.getint('section1', 'k1')  
        #v = config.getfloat('section1', 'k1')  
        #v = config.getboolean('section1', 'k1')  
        print(v)  

    @5)、检查、删除、添加节点

        import configparser  
        config = configparser.ConfigParser()  
        config.read('xxoo.txt', encoding='utf-8')  
        #检查  
        has_sec = config.has_section('section1')  
        print(has_sec)  
        #添加节点  
        config.add_section('SEC_1')  
        config.write(open('xxoo.txt', 'w'))  
        #删除节点  
        config.remove_section("SEC_1")  
        config.write(open("xxoo.txt", 'w'))  

    @6)、检查、删除、设置指定组内的键值对

    import configparser  
    config = configparser.ConfigParser()  
    confgi.read('xxoo.txt', encoding='utf-8')  
    #检查  
    has_opt = config.has_option('section1','k1')  
    print(has_opt)  
    #删除  
    config.remove_option('section1', 'k1')  
    config.write(open('xxoo.txt','w'))  
    #设置  
    config.set('section1','k10','123')  
    config.write(open("xxoo.txt",'w')) 
  • 相关阅读:
    蓝桥杯基础 算法训练 图形显示 (简单模拟,坑)
    越喜欢村上春树,就越懂得生活
    HTML 基础 之 列表标签 () 学习笔记
    HTML 基础 之 段落标签() 学习笔记
    《Norwegain Wood》—— The Beatles
    蓝桥杯基础 算法训练 前缀表达式 (基础语法)
    Python3 使用 urllib 包访问Web网站
    蓝桥杯 算法提高 队列操作 (STL基本操作)
    蓝桥杯 算法提高 11-1 实现strcmp函数 (C语言实现,指针实现)
    蓝桥杯基础 算法训练 矩阵乘法 (模板题)
  • 原文地址:https://www.cnblogs.com/renfanzi/p/5643741.html
Copyright © 2011-2022 走看看