【问题标题】:How to read configurations parameters from an INI file? [closed]如何从 INI 文件中读取配置参数? [关闭]
【发布时间】:2020-01-09 01:42:17
【问题描述】:

我想在 INI 文件中设置一些 excel 文件的路径,以供我在 Python 中的代码读取。你能告诉我怎么给他们打电话吗? 在我的ini 文件中,我有这样的内容:

[common]

default_path = "C:/Users/XX/Desktop/Bk.xlsx/"

【问题讨论】:

    标签: python config ini configparser


    【解决方案1】:

    正如对您问题的评论中提到的,您可以使用configparser module

    import configparser
    
    config = configparser.ConfigParser()
    config.read('config.ini')
    
    xlsx_path = config['common']['default_path']
    

    【讨论】:

    • 我仍然得到 KeyError: 'common' 你知道为什么吗?
    • @Gogo78 你能发布你的完整代码和ini文件吗? python 文件和 ini 文件是否在驱动器上的同一文件夹中?
    【解决方案2】:

    以下是如何读取 .INI 文件的路径并将其添加到 [common] 部分。

        import configparser
    
        config = configparser.ConfigParser()
        config.read('yourfile.INI')
        #set existing key-values in existing section
        config.set('common', 'default_path', 'your_path_to_file.xls')
        with open('yourfile.INI', 'w') as configfile:    
            config.write(configfile)
    

    【讨论】:

    • 或者还有 config.append('common', 'default_path', 'your_path_to_file.xls')
    【解决方案3】:
    1. 要加载 excel 文件,请从 pypi 安装 xlrd。

    安装 xlrd 模块的命令: 点安装 xlrd

    在 config.ini 中不需要 " 所以:

    [general]
    default_path = C:/Users/XX/Desktop/Bk.xlsx/
    

    示例代码:

    import xlrd
    import configparser
    
    #Loading config
    config = configparser.ConfigParser()
    config.read("config.ini")
    Excel_PATH = config["general"]["default_path"]
    
    # Reading an excel file using Python 
    wb = xlrd.open_workbook(Excel_PATH) 
    sheet = wb.sheet_by_index(0) 
    
    # For row 0 and column 0 
    sheet.cell_value(0, 0) 
    

    【讨论】:

      【解决方案4】:

      假设您的config.ini 文件具有以下内容。

      [meta-properties]
      server=localhost
      port=8000
      
      [db-properties]
      name=student
      password=1234
      
      [logs]
      level=warn
      logdirectory=/var/logs/newapp/
      

      这可以通过以下代码读取。文件数据存储为字典格式,以便您可以通过键名访问ini文件的每个属性。

      import configparser
      from pprint import pprint
      
      
      def as_dict(config):
          config_dict = {}
          for section in config.sections():
              config_dict[section] = {}
              for key, val in config.items(section):
                  config_dict[section][key] = val
          return config_dict
      
      
      if __name__ == '__main__':
          conf = configparser.ConfigParser()
          conf.read(['config.ini'], encoding='utf-8')
          pprint(as_dict(conf))
      

      给出这样的输出

      {
        'db-properties': {
          'name': 'student',
          'password': '1234'
        },
        'logs': {
          'level': 'warn',
          'logdirectory': '/var/logs/newapp/'
        },
        'meta-properties': {
          'port': '8000',
          'server': 'localhost'
        }
      }```
      
      

      【讨论】:

        猜你喜欢
        • 2010-09-13
        • 2020-08-08
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 2019-11-02
        • 1970-01-01
        • 2015-11-17
        相关资源
        最近更新 更多