【问题标题】:how to access properties stored in a .properties file in a python file using ConfigParser如何使用 ConfigParser 在 python 文件中访问存储在 .properties 文件中的属性
【发布时间】:2017-02-10 06:38:51
【问题描述】:

我有一个包含数据的 .properties 文件。我正在尝试从 python 文件访问这些属性。

我的属性文件如下所示:

[section1]
header = time, date, name, process_name, location

[section2]
content = timestamp, details

我在 python 中使用 ConfigParser,我想访问这样的属性:

config.get("section1", header[0])
config.get("section2", content[2])

但现在,我收到此错误:“未定义全局名称'header' "

如何解决此错误或如何使用位置编号来引用特定属性?

【问题讨论】:

    标签: python properties config configparser


    【解决方案1】:

    config.get('section1', 'header') 将返回 'time, date, name, process_name, location'。 然后使用split 将其分解为['time', 'date', 'name', 'process_name', 'location']

    print(config.get('section1', 'header').split(', ')[0])
    # time
    
    print(config.get('section2', 'content').split(', ')[1])
    # details
    

    【讨论】:

      猜你喜欢
      • 2021-05-27
      • 2020-11-28
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2011-04-05
      • 1970-01-01
      相关资源
      最近更新 更多