[英]How to make string formatted afterwards
I have a config.yaml
which contains following:我有一个config.yaml
,其中包含以下内容:
SQL1: 'select * from {x1}'
SQL2: 'select * from {x2}'
SQL3: 'select * from {x3}'
SQL4: 'select * from {x2}'
SQL5: 'select * from {x1}'
Then I have my script (example):然后我有我的脚本(示例):
x1 = 'test'
x2 = 'test2'
x3 = 'test3'
for sql in ['SQL1', 'SQL2', 'SQL3', 'SQL4', 'SQL5']:
print(config[sql])
Desired output:所需的 output:
select * from test
select * from test2
select * from test3
select * from test2
select * from test
But this doesn't work.但这不起作用。 I need to tell Python somehow that the string which config['SQL1']
points to is a formatted string like f''
我需要以某种方式告诉 Python config['SQL1']
指向的字符串是一个像f''
这样的格式化字符串
KEY: 'Print my var: {x}'
You can do it easily if the brackets {}
are empty, with the str
method .format
:如果括号{}
为空,您可以使用str
方法.format
:
a = "Print my var: {}"
print(a.format(x))
You can achieve this using re
( regex
= REGular EXpression), and removing x
from the str
ing.您可以使用re
( regex
= REGular EXpression) 并从str
中删除x
来实现此目的。
As I just read from documentation and from @jonrsharpe's comment, you can do it also passing x
as a **kwarg
of .format
:正如我刚刚从文档和@jonrsharpe 的评论中读到的那样,您也可以将x
作为 .format 的**kwarg
.format
:
a = "Print my var: {x}"
print(a.format(x="Variable"))
If you don't know the name of the variable, you can estract it with regex
as suggested above, remove it from the string leaving the {}
empty, and then get its value this way:如果您不知道变量的名称,可以按照上面的建议使用regex
将其提取出来,将其从字符串中删除,使{}
为空,然后通过这种方式获取其值:
print(a.format(globals()[variable_name_you_got_from_regex]))
This might not scale, but based on your example and the current values this would work:这可能无法扩展,但根据您的示例和当前值,这将起作用:
config = {
"SQL1": "select * from {x1}",
"SQL2": "select * from {x2}",
"SQL3": "select * from {x3}",
"SQL4": "select * from {x2}",
"SQL5": "select * from {x1}",
}
x1 = "test"
x2 = "test2"
x3 = "test3"
for sql in ["SQL1", "SQL2", "SQL3", "SQL4", "SQL5"]:
config_line = config[sql]
config_line = config_line.replace('{x1}', x1)
config_line = config_line.replace('{x2}', x2)
config_line = config_line.replace('{x3}', x3)
print(config_line)
The output is: output 是:
select * from test
select * from test2
select * from test3
select * from test2
select * from test
Another option is to use a template, modifying your input to match the template format.另一种选择是使用模板,修改您的输入以匹配模板格式。
from string import Template
config = {
"SQL1": "select * from $x1",
"SQL2": "select * from $x2",
"SQL3": "select * from $x3",
"SQL4": "select * from $x2",
"SQL5": "select * from $x1",
}
replacement_mapping = {
"x1": "test",
"x2": "test2",
"x3": "test3",
}
for sql in ["SQL1", "SQL2", "SQL3", "SQL4", "SQL5"]:
config_line = config[sql]
template = Template(config_line)
print(template.substitute(**replacement_mapping))
Then you can pass a dictionary of the standin variables and their replacement values.然后您可以传递标准变量及其替换值的字典。
What works is to use有效的是使用
'Set {x}, {y} but not z'.format(x=x, y=y, z=z)
. 'Set {x}, {y} but not z'.format(x=x, y=y, z=z)
。
Python will just ignore not used vars like in this example 'z'. Python 将忽略未使用的变量,如本例中的“z”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.