Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigParser options with % in value

I'm trying to parse a .conf file with ConfigParser, and some of the values have a '%' in the value.

Example of the config below:

[global]
    workgroup = WORKGROUP
    server string = %h server (Samba, Ubuntu)

When I parse this using RawConfigParser, it sets server string to

%h server (Samba, Ubuntu)\n\n\n\ndns proxy = no\n\n\n\n\n\n\n\n...REST_OF_CONFIG_SECTION

Where the bunch of \n are blank lines between config options. And when I use the normal ConfigParser, I get an InterpolationSyntaxError with the error

'%' must be followed by '%' or '(', found: '%h server (Samba, Ubuntu)\n\n\n\ndns proxy = no\n\n\n\n\n\n\n\n

Editing the string before passing it to ConfigParser (replacing % with %%) parses it, but does the same issue as RawConfigParser where it parses the rest of the options as a single line.

How am I supposed to properly parse values that have a % in them?

like image 824
cclloyd Avatar asked Feb 03 '26 01:02

cclloyd


1 Answers

Disable interpolation in the normal ConfigParser:

configparser.ConfigParser(interpolation=None)
like image 194
Alexander Rossmanith Avatar answered Feb 04 '26 14:02

Alexander Rossmanith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!