Similar questions out there, but not quite my use case. I would like to use a dictionary reference to replace variables within a string. In this case, the string is a sql statement, but not too relevant.
SQL with variables {}-
qry = """SELECT
SUM(CASE WHEN {{cols_1}} is null AND {{cols_2}} > 0 THEN 1 ELSE 0 END) AS output
FROM mytable"""
dictionary -
dict = {'cols_1': 'id', 'cols_2': 'weather'}
So then it would end up like this -
qry = """SELECT
SUM(CASE WHEN id is null AND weather > 0 THEN 1 ELSE 0 END) AS output
FROM mytable"""
And I'd like to replace cols_1 and cols_2 with dictionary values. But I'm not sure how to do this?
def substitute_values(qry, dict):
if dict:
qry = qry.replace('{{cols_1}}','{{cols_2}}'),dict[]
return qry
After spinning my wheels a bit, appreciate any guidance.