如何在使用 ConfigParser 将 Python 字符串作为环境变量读取时转义字符
How to escape charachters in a Python string while reading it as an environment variable using ConfigParser
我收到以下错误---
"configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%dCUD'"
I want to read credentials which are set as an environment variables as well as present in .config file. So I am using pythons ConfigParser as follows.
import configparser as cp
from configparser import ConfigParser
config = ConfigParser(os.environ) #<-- this enables ConfigParser to read from environment variable
config.read(CONFIG_FILEPATH) #<--- this is to read from .confog file
My .config file is like this--
[Postgres]
Postgres.host = XXX.com
Postgres.METADATADB=pda-study-beta
Postgres.DATAREFRESHDB=
Postgres.user= %(XXXX)s
Postgres.password = %(Postgres_Pass)s
anything included inside *%()s* means it is being read from environment variable.
I read it in my scripts as follows:
config.get('Postgres','Postgres.password')
It works fine for all but for password, its throwing me following error in password section--
"configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%dCUD'"
Its because my password contains '%' character. e.g.(xxx%dCUDxx)
Does any one have any idea how do I handle this. We can escape % with another %, but in my case, I am reading password from environment variable, so cant manipulate it.
Can anyone please help me resolve this ?
configparser
模块文档含糊不清,但如果您查看源代码,您会发现在方法 _interpolate_some
中,%
扩展是递归的。也就是说,%
也对内插值进行了扩展——您的示例中的密码。
I am reading password from environment variable, so cant manipulate it.
您确实可能不想改变全局环境变量。但是没有什么能阻止你制作副本并改变该副本。例如,
config = ConfigParser({k: v.replace('%', '%%') for k, v in os.environ.items()})
我收到以下错误--- "configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%dCUD'"
I want to read credentials which are set as an environment variables as well as present in .config file. So I am using pythons ConfigParser as follows.
import configparser as cp
from configparser import ConfigParser
config = ConfigParser(os.environ) #<-- this enables ConfigParser to read from environment variable
config.read(CONFIG_FILEPATH) #<--- this is to read from .confog file
My .config file is like this--
[Postgres]
Postgres.host = XXX.com
Postgres.METADATADB=pda-study-beta
Postgres.DATAREFRESHDB=
Postgres.user= %(XXXX)s
Postgres.password = %(Postgres_Pass)s
anything included inside *%()s* means it is being read from environment variable.
I read it in my scripts as follows:
config.get('Postgres','Postgres.password')
It works fine for all but for password, its throwing me following error in password section--
"configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%dCUD'"
Its because my password contains '%' character. e.g.(xxx%dCUDxx)
Does any one have any idea how do I handle this. We can escape % with another %, but in my case, I am reading password from environment variable, so cant manipulate it.
Can anyone please help me resolve this ?
configparser
模块文档含糊不清,但如果您查看源代码,您会发现在方法 _interpolate_some
中,%
扩展是递归的。也就是说,%
也对内插值进行了扩展——您的示例中的密码。
I am reading password from environment variable, so cant manipulate it.
您确实可能不想改变全局环境变量。但是没有什么能阻止你制作副本并改变该副本。例如,
config = ConfigParser({k: v.replace('%', '%%') for k, v in os.environ.items()})