【问题标题】:Manipulate configuration files with ConfigParser lib on python在 python 上使用 ConfigParser lib 操作配置文件
【发布时间】:2016-07-14 09:21:09
【问题描述】:

我有一个包含多个配置的conf文件,我需要自动解析信息以便创建一个易于使用的界面。

我举一个我的配置部分的例子:

[OutOfHoursAlert]
comment_max_hour = Max Hour
max_hour = 20
comment_min_hour = Min Hour
min_hour = 8
comment_freq = Frequency of Plugin Executation
frequency = 10

所有部分都阅读了目标(这没关系,我只是轻松完成)

然后我不知道该怎么做的困难部分是在我的终端界面上显示评论,以及更改评论下方选项的选项。

像这样:

Menu to configure OutOfHoursAlert
  Max Hour (read from comment) : (then show the field to edit, in this case max_hour)
  Min Hour (read from comment) : (then show the field to edit, in this case min_hour)
  FRequency of PLugin Executation (read from comment) : (then show the field to edit, in this case frequency)

要阅读所有插件,我有一个简单的代码: 导入配置解析器

import plugnplay
from plugins.PluginAlert import PluginAlerter

plugnplay.plugin_dirs = ['./plugins']
plugnplay.load_plugins()
plugins = PluginAlerter.implementors()

templist = []

for x in plugins:
    temp = str(x).split('.')
    templist.append(temp[1])

config = ConfigParser.ConfigParser()
config.readfp(open('./conf.ini'))

for x in templist:
    print "\nThe Configurations for plugin "+x+" are:\n"
    print dict(config.items(x))

我创建了一个包含我想要的所有部分的字典,但我喜欢像我之前所说的那样做,有什么办法吗?任何提示?谢谢

【问题讨论】:

    标签: python python-2.7 configparser


    【解决方案1】:

    您可以使用new_value = input(comment+": ") 从用户那里获取新值。

    如果您想构建一个允许实际编辑变量、移动光标等的高级控制台程序,您可能需要查看 python 标准库的 curses 模块。

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 2011-05-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多