Configparser and string with %

2020-06-30 04:52发布

Stupid question with (for sure) simple answer...

I am using configparser to read some strings from a file. When the string has the '%' symbol ($%& for example) it complains:

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

Anybody familiar with this?

Thanks!

标签: python
3条回答
Evening l夕情丶
2楼-- · 2020-06-30 05:08

If you don't want environment variable substitution, then use RawConfigParser, not ConfigParser.

查看更多
The star\"
3楼-- · 2020-06-30 05:14

Write two %:

V = ('%%', 'MHz', 'GHz')

result:

('%', 'MHz', 'GHz')
查看更多
▲ chillily
4楼-- · 2020-06-30 05:21

In newer Python versions, use

configParser = configparser.ConfigParser(interpolation=None)

This disables interpolation.

Note that RawConfigParser is a legacy variant. From the python docs:

Consider using ConfigParser instead which checks types of the values to be stored internally. If you don’t want interpolation, you can use ConfigParser(interpolation=None).

查看更多
登录 后发表回答