Is it possible to temporarily disable Python's string interpolation?

空扰寡人 提交于 2019-12-05 00:25:44

Did you try to escape percents with %%?

You might wanna use ConfigParser.RawConfigParser instead of ConfigParser.ConfigParser. Only the latter does magical interpolation on config values.

EDIT:

Actually, using ConfigParser.SafeConfigParser you'll able to escape format strings with an additional % percent sign. This example should be working then:

{
    "log_level":logging.debug,
    "log_name":"C:\\Temp\\logfile.log",
    "format_string":
        "%%(asctime)s %%(levelname)s: %%(module)s, line %%(lineno)d - %%(message)s"
}
Bharati Desai

You can also use interpolation set to None.

config = ConfigParser(strict=False, interpolation=None)

(I am using Python 3.6.0)

There's RawConfigParser which is like ConfigParser without the interpolation behaviour. If you don't use the interpolation feature in any other part of the configuration file, you can simply replace ConfigParser with RawConfigParser in your code.

See the documentation of RawConfigParser for more details.

What is the problem with the code above? Interpolation is only performed if the % operator is applied to the string. If you don't use % you can use the formatting string like any other string.

What version of Python are you using? An upgrade to 2.6.4 may help, see

http://bugs.python.org/issue5741

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!