简体   繁体   English

如何在configobj值中转义%符号?

[英]How to escape a % sign in a configobj value?

I'd like to use both parse time and runtime interpolation for values of a configobj configuration file. 我想对configobj配置文件的值使用解析时间和运行时插值。 The easiest way to do simple string interpolation in Python is "%(foo)s" % somedict . 在Python中进行简单字符串插值的最简单方法是"%(foo)s" % somedict Unfortunately configobj uses the same interpolation mechanism and I have not yet found a way to escape it. 不幸的是,configobj使用相同的插值机制,我还没有找到逃脱它的方法。 Ideally my value would look like: 理想情况下,我的价值看起来像:

othervar = foo
someconfigobjkey = %(othervar)s %%(runtimevar)s

However configobj tries (and fails) to replace the second variable. 然而,configobj尝试(并失败)替换第二个变量。 Another way to answer this question is to provide a different (configobj) way to do both parse time and runtime interpolation. 回答这个问题的另一种方法是提供一种不同的(configobj)方法来同时进行解析时间和运行时插值。

Things may have changed since this question was posted, but I've discovered that you can escape using the '%' char. 自发布此问题以来,情况可能已发生变化,但我发现您可以使用'%'字符进行转义。 This worked for me in Python 3. 这在Python 3中对我有用。

In my config.ini file: 在我的config.ini文件中:

[LOGGING]
format = %%(asctime)-15s %%(message)s

In my python script file: 在我的python脚本文件中:

import configparser

# Load configuration
config = configparser.ConfigParser()
config.read("config.ini")

log_config = config['LOGGING']

print(log_config['format']) # prints out '%(asctime)-15s %(message)s'

Hope it helps someone in the future (: 希望它能在将来帮助某人(:

Yes, unfortunately configobj does not follow normal standard Python formatting rules, so %% , which would otherwise be the correct way to escape a percent sign, fails. 是的,遗憾的是,configobj不遵循正常的标准Python格式规则,因此%% ,否则将是正确的方法来逃避百分号,将失败。 Seems like a bit of a design flaw to me. 对我来说似乎有点像设计缺陷。

The only way I can think of to have a literal %(foo)s sequence in a configobj value would be to ensure part of that sequence is itself generated by a replacement: 我可以想到在configobj值中有一个文字%(foo)s序列的唯一方法是确保该序列的一部分本身由替换生成:

>>> import configobj
>>> c= configobject.ConfigObj()
>>> c['foo']= 'bar'
>>> c['pc']= '%'
>>> c['v']= '%(foo)s %(pc)s(foo)s'
>>> c['v']
'bar %(foo)s'

Try using '%'+'(foo)s'. 尝试使用'%'+'(foo)s'。 That worked for me when dealing with ConfigParser. 在处理ConfigParser时,这对我有用。

Digging deeper in the documentation I found a solution that fits better than the one proposed by bobince: Use a different interpolation engine, such as interpolation="template" . 在文档中深入挖掘我发现了一个比bobince提出的更好的解决方案:使用不同的插值引擎,例如interpolation="template" It has two advantages. 它有两个优点。

  1. Does not interfere with % signs. 不会干扰%迹象。
  2. Supports escaping. 支持逃避。 ( $$ gets interpolated to $ .) $$被内插到$ 。)

The example would look like: 示例如下:

othervar = foo
someconfigobjkey = $othervar %(runtimevar)s

New version of the answer: 新版本答案:

You could use "".format() (Python 2.6 or above) or string.Template (Python 2.4 or above) for the runtime interpolation -- both of which would eliminate the conflict. 您可以使用“.format()(Python 2.6或更高版本)或string.Template(Python 2.4或更高版本)进行运行时插值 - 这两种方法都可以消除冲突。

(The old version assumed the use of the standard ConfigParser module, hence the comments.) (旧版本假定使用标准的ConfigParser模块,因此注释。)

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

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