简体   繁体   English

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

[英]How to use % signs in configparser python?

I am writing a config.ini file for my python project. 我正在为我的python项目编写config.ini文件。 I want to mention the standard date format of a column. 我想提到一列的标准日期格式。

Something like this 像这样

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

But it interpolates something else and errors out. 但这会插值其他内容并出错。 Could someone please let me know how to use this? 有人可以让我知道如何使用吗?

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'

You have three options: 您有三种选择:

  • Disable interpolation, or pick a different interpolation handler. 禁用插值,或选择其他插值处理程序。 You can set the interpolation option to None to disable interpolation for the whole file: 您可以将interpolation选项设置为“ None以禁用整个文件的插值:

     ConfigParse(interpolation=None) 

    See the Interpolation of values section . 参见值插值部分

  • Access that specific value with interpolation disabled, setting raw=True for the ConfigParser.get() method : 在禁用插值的情况下访问该特定值,为ConfigParser.get()方法设置raw=True

     config.get('section', 'prod_db_end_date_format', raw=True) 
  • Double the % characters to %% : %字符加倍至%%

     prod_db_end_date_format = %%Y-%%m-%%d 

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

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