【发布时间】:2014-12-12 05:47:20
【问题描述】:
我在 Python 2.6 中使用ConfigParser 模块,我希望从我的配置文件中加载格式化字符串。此类字符串的示例如下:
[Section Name]
#in the config file
#tmp_string has value 'string'
my_string = 'This is a %s' % tmp_string
有没有一种很好的方法可以将这些字符串保存在配置文件中?在加载配置文件之前,我不会知道字符串的值是什么,所以我无法评估它然后将其保存到文件或类似的东西中。显然,此刻,当我加载字符串并打印出来时,我得到以下信息:
#config is the ConfigParser
my_string = config.get('Section Name', 'my_string')
print my_string
>>>'This is a %s' % tmp_string
我很想得到输出:
>>> This is a string
你是如何做到这一点的?我更愿意留在ConfigParser,但另一种选择可能是可以接受的。 (我知道你不能只打印字符串并让它以你想要的方式神奇地出现,我只是想展示我想要做什么。)
【问题讨论】:
标签: python string-formatting python-2.x configuration-files configparser