如何在configparser python中使用%符号?

时间:2017-09-11 12:44:59

标签: python configparser

我正在为我的python项目编写config.ini文件。我想提一下列的标准日期格式。

像这样的东西

prod_db_end_date   = 2017-01-01
prod_db_end_date_format = %Y-%m-%d

但是它会插入其他内容并输出错误。有人可以告诉我如何使用它吗?

Error traceback - File "C:\ProgramData\Anaconda3\lib\configparser.py", line 443, in _interpolate_some
      "found: %r" % (rest,))
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%Y-%m-%d'

1 个答案:

答案 0 :(得分:3)

您有三种选择:

  • 禁用插值,或选择不同的插值处理程序。您可以将//div[@class='Tips' and text()='\u00a0'] 选项设置为interpolation以禁用整个文件的插值:

    None

    请参阅Interpolation of values section

  • 在禁用插值的情况下访问该特定值,为ConfigParser.get() method设置ConfigParse(interpolation=None)

    raw=True
  • config.get('section', 'prod_db_end_date_format', raw=True) 个字符加倍%

    %%