繁体   English   中英

在python上使用ConfigParser lib处理配置文件

[英]Manipulate configuration files with ConfigParser lib on python

我有一个带有几种配置的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 ConfigParser

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))

我用我想要的所有章节创建字典,但是我喜欢像我之前讲过的那样,有什么办法吗? 有小费吗? 谢谢

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM