configparser.InterpolationMissingOptionError: Bad value substitution: option 'log_format' in section 'LOG' contains an interpolation key 'asctime' which is not a valid option name. Raw value: '%(ascti

本人虽然已经开通博客4个月有余,但是一直没啥东西可写,用Python做测试也有很长时间了,遇到的问题也挺多,但网上基本都能找到相应的解决方法,本来我也不想写东西,因为很多内容都是重复的。但是今天遇到这个问题算是我遇到多次却没能完全解决的,因为经常会有类似的功能要做,不完美解决的话总是挺难受的,因为在网上搜索到的答案经常对自己是没用的,老外出了类似的问题,我看了之后也没有完美解决。 在Python中我们经会使用configparser这个模块来读取配置文件,而且一般情况都是使用ConfigParser这个方法,但是当我们配置中有%(filename)s这种格式的配置的时候,可能会出现以下问题:

Traceback (most recent call last): File "D:/test_wechat/util/read_config.py", line 39, in log_file = rc.get_config('LOG','log_format') File "D:/test_wechat/util/read_config.py", line 33, in get_config value = self.cf.get(cfg_name, params_name) File "E:\Python36\lib\configparser.py", line 800, in get d) File "E:\Python36\lib\configparser.py", line 394, in before_get self._interpolate_some(parser, option, L, value, section, defaults, 1) File "E:\Python36\lib\configparser.py", line 434, in _interpolate_some option, section, rawval, var) from None configparser.InterpolationMissingOptionError: Bad value substitution: option 'log_format' in section 'LOG' contains an interpolation key 'asctime' which is not a valid option name. Raw value: '%(asctime)s %(name)s %(filename)s [line:%(lineno)d] - %(levelname)s: %(message)s'

在网上找了很多解决方法后,结果任然是一样的!气不气?好气的哟!!!

后来我直接进入到configparser文件中发现了这个东西:RawConfigParser!!!

于是果断将configparser方法换成了RawConfigParser方法,上面的问题就pass掉了。

所以有些时候还是得多看看原模块的方法定义才行啊!

亲测有效,不喜勿喷!

posted on 2018-03-23 17:04  zooe  阅读(1484)  评论(0)    收藏  举报

导航