簡體   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