【发布时间】:2019-02-20 02:02:40
【问题描述】:
我需要指针来使用 python 2.7 ConfigParser 解析 INI 文件,如下所示:
[google]
www.google.com domain_name=google location=external
[yahoo]
www.yahoo.com domain_name=yahoo location=external
这是我试图做的:
Config = ConfigParser.ConfigParser()
try:
Config.read("test.ini")
except Exception:
pass
options = Config.options('google')
for option in options:
print("Option is %s" % option)
print("Value for %s is %s" % (option, Config.get('google', option)))
这就是输出:
Option is www.google.com domain_name
Value for www.google.com domain_name is google location=external
我希望能够将 www.google.com 和剩余的 key=value 对(domain_name=google; location=external) 解析为字典的每个部分的同一行。对此表示赞赏。
【问题讨论】:
-
似乎问题在于 ConfigParser 对文件的可能结构有不同的假设。请问你的目标是什么?也许你可以直接使用 Ansible?
标签: python-2.7 ini configparser ansible-inventory