ConfigParser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: "%&'" 解决方案

前言

在写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')

报错解决!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值