使用python脚本编辑配置文件 [英] Editing the configuration file using python script
问题描述
我有一个如下配置文件
....
....
MIN_TOKENS='30 50' # can be a sequence of integers
STRIDE='2 0' # can be a sequence of integers
SIMILARITY='1.0 0.95' # can be a sequence of values <= 1
....
....
我需要将参数编辑为上述每个配置(如果需要),并通过运行应用程序来测试所编辑的参数是否适合我的需求.我需要使用python脚本自动执行此过程.
I need to edit the parameters to each of the above configurations(if needed) and test if the edited parameter suits my need by running my application. I need to automate this process using python script.
例如: 我需要将配置文件更改为
For example: I need to change the config file to something like
....
....
MIN_TOKENS='30 50'
STRIDE='2'
SIMILARITY='0.75'
....
....
,然后运行我的应用程序.但是,参数应该在范围内自动生成,我不应该手动输入它们.我对python不熟悉.有人可以告诉我如何解决这个问题.
and then run my application. However, the parameters should be automatically generated within range and I shouldn't manually feed them. I'm not familiar with python. Could someone tell me how to approach this.
推荐答案
是的. 您的config.ini将如下所示
yes you can do. your config.ini will be something like below
[Device]
MIN_TOKENS = "34 45"
STRIDE = 45
SIMILARITY = '0.75'
conf.py
from ConfigParser import SafeConfigParser
config_file = "config.ini"
parser = SafeConfigParser()
parser.optionxform = str
parser.read(config_file)
def set_config(section, option, value):
if parser.has_section(section):
parser.set(section, option , value)
with open(config_file, "w") as conf_file:
parser.write(conf_file)
return True
set_config('Device','MIN_TOKENS','"34 20"')
输出
[Device]
MIN_TOKENS = "34 20"
STRIDE = 45
SIMILARITY = '0.75'
这篇关于使用python脚本编辑配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!