zoukankan      html  css  js  c++  java
  • Python读取ini配置文件

    db_config.ini  
    [baseconf]  
    host=127.0.0.1  
    port=3306  
    user=root  
    password=root  
    db_name=evaluting_sys  
    [concurrent]  
    processor=20  

    python代码

     1 对应的python代码
     2 import sys,os  
     3 import ConfigParser  
     4   
     5 class Db_Connector:  
     6   def __init__(self, config_file_path):  
     7     cf = ConfigParser.ConfigParser()  
     8     cf.read(config_file_path)  
     9   
    10     s = cf.sections()  
    11     print 'section:', s  
    12   
    13     o = cf.options("baseconf")  
    14     print 'options:', o  
    15   
    16     v = cf.items("baseconf")  
    17     print 'db:', v  
    18   
    19     db_host = cf.get("baseconf", "host")  
    20     db_port = cf.getint("baseconf", "port")  
    21     db_user = cf.get("baseconf", "user")  
    22     db_pwd = cf.get("baseconf", "password")  
    23   
    24     print db_host, db_port, db_user, db_pwd  
    25   
    26     cf.set("baseconf", "db_pass", "123456")  
    27     cf.write(open("config_file_path", "w"))  
    28 if __name__ == "__main__":  
    29   f = Db_Connector("../conf/db_config.ini")  

    通用模块:支持命令行+import两种形式

    import sys,os,time  
    import ConfigParser  
      
      
    class Config:  
        def __init__(self, path):  
            self.path = path  
            self.cf = ConfigParser.ConfigParser()  
            self.cf.read(self.path)  
        def get(self, field, key):  
            result = ""  
            try:  
                result = self.cf.get(field, key)  
            except:  
                result = ""  
            return result  
        def set(self, filed, key, value):  
            try:  
                self.cf.set(field, key, value)  
                cf.write(open(self.path,'w'))  
            except:  
                return False  
            return True  
                  
                  
      
    def read_config(config_file_path, field, key):   
        cf = ConfigParser.ConfigParser()  
        try:  
            cf.read(config_file_path)  
            result = cf.get(field, key)  
        except:  
            sys.exit(1)  
        return result  
      
    def write_config(config_file_path, field, key, value):  
        cf = ConfigParser.ConfigParser()  
        try:  
            cf.read(config_file_path)  
            cf.set(field, key, value)  
            cf.write(open(config_file_path,'w'))  
        except:  
            sys.exit(1)  
        return True  
      
    if __name__ == "__main__":  
       if len(sys.argv) < 4:  
          sys.exit(1)  
      
       config_file_path = sys.argv[1]   
       field = sys.argv[2]  
       key = sys.argv[3]  
       if len(sys.argv) == 4:  
          print read_config(config_file_path, field, key)  
       else:  
          value = sys.argv[4]  
          write_config(config_file_path, field, key, value)  
  • 相关阅读:
    向量求导几则公式备忘
    电脑硬件接触不良
    caffe编译新问题
    faster-rcnn 目标检测 数据集制作
    py-faster-rcnn 的makefile.config 注意事项
    ubuntu14.04 python + opencv 傻瓜式安装解决方案
    轻量级神经网络平台tiny-dnn实践
    OpenMP 并行编程
    React在Render中使用bind可能导致的问题
    为了cider,尝试emacs的坑
  • 原文地址:https://www.cnblogs.com/jinjiangongzuoshi/p/5020273.html
Copyright © 2011-2022 走看看