Reputation: 43
I am trying to read from ini file and replace it with os environment variables.
My ini file:
[svc1]
host=%(TEST_IP)s
port=%(TEST_PORT)s
database=test_db
user=test
connect_timeout=3
My python file:
from configparser import ConfigParser
import os
print(os.environ['TEST_IP'])
print(os.environ['TEST_PORT'])
parser = ConfigParser(os.environ)
parser.read('test.ini')
print(parser.items('svc1'))
Output:
192.168.1.1
8080
Traceback (most recent call last):
File "test.py", line 8, in <module>
print(parser.items('svc1'))
File "/usr/lib/python3.8/configparser.py", line 859, in items
return [(option, value_getter(option)) for option in orig_keys]
File "/usr/lib/python3.8/configparser.py", line 859, in <listcomp>
return [(option, value_getter(option)) for option in orig_keys]
File "/usr/lib/python3.8/configparser.py", line 855, in <lambda>
value_getter = lambda option: self._interpolation.before_get(self,
File "/usr/lib/python3.8/configparser.py", line 395, in before_get
self._interpolate_some(parser, option, L, value, section, defaults, 1)
File "/usr/lib/python3.8/configparser.py", line 442, in _interpolate_some
raise InterpolationSyntaxError(
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%s %s'
What am I doing wrong here?
My python version: Python 3.8.10
Upvotes: 1
Views: 2835
Reputation: 950
Use double %
to escape error.
Or another way is use configparser.RawConfigParser()
to get the raw data.
Use your code to example. Change
from configparser import ConfigParser
import os
print(os.environ['TEST_IP'])
print(os.environ['TEST_PORT'])
parser = ConfigParser(os.environ)
parser.read('test.ini')
print(parser.items('svc1'))
to
from configparser import RawConfigParser
import os
print(os.environ['TEST_IP'])
print(os.environ['TEST_PORT'])
parser = RawConfigParser(os.environ) # Change to `RawConfigParser` method
parser.read('test.ini')
print(parser.items('svc1'))
and it will get the raw string without double %
Upvotes: 1
Reputation: 527
Python treats the %
character differently, when part of the contents of a string can cause this issue.
As per the helpful Python error message (see traceback) rather than escape it, just double it within your original string to %%
A useful way to obfuscate plain text passwords from prying eyes!
FYI configparser.py also does not like the #
standard comment character
or empty values so I use = []
As mentioned here:
https://github.com/mobeigi/fb2cal/issues/37
https://github.com/flakshack/pyPerfmon/issues/1
Upvotes: 2