zoukankan      html  css  js  c++  java
  • python -config-ini 配置文件的使用

    参考:  https://www.cnblogs.com/hanmk/p/9843136.html

        https://www.cnblogs.com/wangjian1226/p/10448752.html

    python使用自带的configparser模块用来读取配置文件,配置文件的形式类似windows中的ini文件

    在使用前需要先安装该模块,使用pip安装即可

    2.configparser读取文件的基本方法

    依赖: pip install  configparser

    中文乱码问题:  j解决

     conf.read(path,encoding="utf-8")

    config.ini

    [test]
    test = myttwar
    rndis = 0
    aaa = 0
    [kafka]
    host = 192.168.25.147:9092

    ConfigUtils.py:(推荐使用)

    '''
    @author: sea
    @time : 2019-10-14
    @need : pip install configparser
    ConfigUtils.py
    
    '''
    #coding:utf-8
    from configparser import ConfigParser
    
    class ConfigUtils(ConfigParser,object):
        def __init__(self,path):
            super(ConfigUtils,self).__init__()
            self.path = path
            self.read(path,encoding="utf-8")

    def getSection(self,section): # sections = self.sections() #sct = self.options(section)#get all key ,retuen a list
          sct=self.items(section)#get all key -value dict
    return sct def reset(self,section): '''#重置某个域下所有key的value为0''' key_list = self.options(section) f = open(self.path,'w') for k in key_list: self.set(section,k,0) self.write(f) f.close() def getValue(self,section,key): value = self.get(section,key) print ("ConfigUtils get value "+str(value)) return value def setValue(self,section,key,value): f = open(self.path,'w') self.set(section,key,value) self.write(f) f.close() def getValue(path,section,key): cf=ConfigUtils(path) v = cf.getValue(section,key) return v def getSection(path): cf=ConfigUtils(path) cf.getValue() def setValue(path,section,key,value): cf=ConfigUtils(path) cf.setValue(section, key, value) if __name__ == '__main__': cf=ConfigUtils('../../config.ini') print(cf) # c=cf.getSection("kafka") # print(c) setValue('../../config.ini', "kafka", "test","test") v = getValue('../../config.ini', "kafka", "test") print(v) # 使用介绍 # cf=ConfigUtils('../../config.ini') #加载配置文件 config.ini # cf.getValue('kafka','host') # 读取某个域下的key值 # cf.getValue('test','test1','1000') #设置某个域下的key所对应的value值,这里最好设置字符串不然再读会报错,具体原因没探究 # for k in cf.options('milestoneCode'): #获取某个域下的所有key # print( k) # cf.reset('test') #重置某个域下所有key的value为0 # '''

    方式2:直接定义方法:
    ConfigUtils.py

    '''
    @author: sea
    @time : 2019-10-14
    @need : pip install configparser
    ConfigUtils.py
    '''
    import configparser
    
    DEFAULT_PATH="config.ini"
    
    def getSection(path,section):
        '''
          get section from path/xxx.ini
        '''
        conf = configparser.ConfigParser()
        conf.read(path,encoding="utf-8")
    #     sections = conf.sections()#all sections
    #     sct = conf.options(section) #get all key
        sct = conf.items(section)#get key==value
        return sct
    
    
    def getValue(path,section,key):
        '''
          get value  from path/xxx.ini  [section]  key=value
        '''
        conf = configparser.ConfigParser()
        conf.read(path,encoding="utf-8")
        value=conf.get(section, key)
        return value
    
    def getV(section,key):
        '''
          get value  from  DEFAULT_PATH/xxx.ini  [section]  key=value
        '''
        path=DEFAULT_PATH
        conf = configparser.ConfigParser()
        conf.read(path,encoding="utf-8")
        value=conf.get(section, key)
        return value
    
    
    def setValue(path,section,key,value):
        '''
          get value  from path/xxx.ini  [section]  key=value
        '''
        conf = configparser.ConfigParser()
        conf.read(path,encoding="utf-8")
        conf.set(section, key,value)
    
        
    def setv(path,section,key,value):
        '''set key : value  into  path/xxx.ini   [section]  key:value'''
        conf = configparser.ConfigParser()
        conf.read(path)
        f = open(path,'w')
        conf.set(section,key,value)
        conf.write(f)
        f.close()
    
    
    
    
    if __name__ == '__main__':
    #     ../../config.ini"
    #     s = getSection("../../config.ini","kafka")
    #     print(s)
        setv("../../config.ini","kafka","qqq","ewerewre")
        v=getValue("../../config.ini","kafka","ww")
        print(v)
        
        
        
  • 相关阅读:
    Python 的 IDLE 编辑器
    Android中如何在Eclipse中关联源代码?(图文)
    HTMl5的存储方式sessionStorage和localStorage详解
    HTML的 <u> 标签
    CSS巧妙实现分隔线的几种方法
    关于ajax跨域请求(cross Domain)
    JQuery中$.ajax()方法参数都有哪些?
    最优雅,高效的javascript字符串拼接
    深入学习JavaScript: apply 方法 详解(转)——非常好
    jQuery.ajax() 函数详解
  • 原文地址:https://www.cnblogs.com/lshan/p/11675029.html
Copyright © 2011-2022 走看看