2

I want to replace multiple values in a list of dict. Example:

# 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

1 Answer 1

3

There is a format-method for strings but you need to adjust the variable names:

# 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}'}]

I adjusted the names in and of you input-array so that they matched the signature of your string. And now I'll show you how you can use the format:

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

to get your desired result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.