• 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)
        
        
        
  • 相关阅读:
    PHP关于VC11,VC9,VC6以及Thread Safe和Non Thread Safe版本选择的问题
    团队展示
    测试与优化:设计模式的应用和初次尝试多线程
    结对编程,三年级混合运算
    一个大气又可爱的标题
    随笔
    C#操作word类文件
    TypeError: Object of type 'int32' is not JSON serializable ——已解决
    Error: UserWarning: Ignoring URL... 已解决
    tensorflow(二)----线程队列与io操作
  • 原文地址:https://www.cnblogs.com/lshan/p/11675029.html
Copyright © 2020-2023  润新知