gpt4 book ai didi

python - 在 Python 3 中替换字典中的多个值

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:33 25 4
gpt4 key购买 nike

我想替换字典列表中的多个值。示例:

# this will set the schema and table dynamically based on the input.
input = {'schema_name': 'test_schema', 'stage_table': 'test_table'}
# need to replace the schemaname and stagetable from the input dict
output = [{id:100, 'query':'SELECT DISTINCT column FROM ${SchemaName}.${StageTable}'}]
# desired output
final_output = select distinct column_name from test_schema.test_table

最佳答案

字符串有一个format方法,但是你需要调整变量名:

# this will set the schema and table dynamically based on the input.
fill = {'schema_name': 'test_schema', 'stage_table': 'test_table'}
# need to replace the schemaname and stagetable from the input dict
output = [{id:100, 'query':'SELECT DISTINCT column FROM ${schema_name}.${stage_table}'}]

我调整了 input 数组中和你的名字,以便它们与你的字符串的签名相匹配。现在我将向您展示如何使用 format:

output[0]['query'].format(**fill)
#'SELECT DISTINCT column FROM $test_schema.$test_table'

得到你想要的结果。

关于python - 在 Python 3 中替换字典中的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36066912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com