I have a sql query txt file i would like to append to (at the top).
The items i want to add are from this yml file:
data:
START_DT: '202001'
END_DT: '202104'
i call this yml file in my python script using:
import yaml
with open("data.yml", "r") as ymlfile:
cfg = yaml.load(ymlfile)
the top of my sql file is:
set START_DT='202001';
set END_DT='202104';
i want to change the top of the sql file such that whatever is in the config yaml file for strt date and end date will change the value of the start_dt, end_dt in the sql query. How can i replace the values after SET for each variable (str date, end date) whilst using only the values from the config?
If i cannot replace the values after START_DT then another option is to remove the SET statments and rewrite from scratch using config.yml variables but if i use this way how can i make the dates strings and how can i append this to top of the sql file or create a new tmp file?:
for k,v in cfg['data'].items():
print("SET" ,str(k)+':'+ v)
prints:
SET START_DT:201901
SET END_DT:202104
but the dates aren't in "'" i would prefer: SET START_DT:'201901'. Again i am not sure how i can append the above print statment to top if the txtfile/sql file.
Any suggestions?