[英]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')
將返回'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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.