前言
在写python程序读取配置文件的时候,遇到了一个错误,记录下解决方案。
错误如下:
ConfigParser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: "%&'"
代码详情
读取read_ini.ini时由于我的ini文件中内容如下:
# 不同ip作为key值,不同命令作为value值
[cmd]
10.10.111.1 = cd $(locate taskmngdomain1/nohuplogs) ; tail -50 *log*`date +%F`*
10.10.111.2 = cd $(locate taskmngdomain2/nohuplogs) ; tail -50 *log*`date +%F`*
import configparser
cf= configparser.ConfigParser()
cf.read('read_ini.ini')
ip_128 = cf.get('cmd','10.10.111.1')
当代码运行到 ip_128 = cf.get(‘cmd’,‘10.10.111.1’)这行,抛出了ConfigParser.InterpolationSyntaxError: ‘%’ must be followed by ‘%’ or ‘(’, found: "%&’"的错误。
大致意思是,因为ini的内容中包含了%号这种特殊符号。
解决方案
换一种方式进行读取配置文件
原本代码:cf= configparser.ConfigParser()
替换成:
cf = configparser.RawConfigParser()
import configparser
cf = configparser.RawConfigParser()
cf.read('read_ini.ini')
ip_128 = cf.get('cmd','10.10.111.1')
报错解决!