如何使用 ConfigParser 访问存储在 python 文件中的 .properties 文件中的属性

how to access properties stored in a .properties file in a python file using ConfigParser

我有一个包含数据的 .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' “

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

config.get('section1', 'header') 将 return '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