I have to replace a key with value in python script and i am reading key from Config file to create a url.
for example: https://xyz/prefix=dcm_accountclick_201903
Config File:
go_req=https://xyz/prefix=dcm_accountclick_,yday_mnth
go_key=yday_mnth,
Script:
yday_date = datetime.datetime.today() - timedelta(days=1)
last_date_fmt = datetime.datetime.strftime(yday_date,'%Y%m%d')
yday_mnth = last_date_fmt[:6]
reqItems = envvars.list['go_req'](here my envars script reads the go_req from config file)
reqKeys = envvars.list['go_key'](here my envars script reads the go_req from config file)
ur= reqItems.split(',')
keys= reqKeys.split(',')
req_url= ''
for i in ur:
for j in keys:
if (str(i) == str(j)):(here if yday_mnth in url matches with key then i will try to add the value which i am generating in script)
req_url += i(here eventhough it matches yday_mnth==yday_mnth am getting output as below)
https://xyz/prefix=dcm_accountclick_ yday_mnth
so i am not sure why its not taking the value of the variable yday_mnth and if i give as below then its working however i want my script to be generic.
req_url += yday_mnth
If I understood correctly, you want to output the value of yday_mnth
when there's a match between reqItems
and reqKeys
, and then append it to the URL?
import datetime
yday_date = datetime.datetime.today() - datetime.timedelta(days=1)
last_date_fmt = datetime.datetime.strftime(yday_date,'%Y%m%d')
yday_mnth = last_date_fmt[:6]
reqItems = "https://xyz/prefix=dcm_accountclick_,yday_mnth"
reqKeys = "yday_mnth,"
ur= reqItems.split(',')
keys= reqKeys.split(',')
req_url= ''
for i in ur:
for j in keys:
if (str(i) == str(j)):
req_url += yday_mnth
print(req_url)
print(ur[0] + req_url)
Output:
201903
https://xyz/prefix=dcm_accountclick_201903
But a much simpler implementation for this would be:
import datetime
yday_date = datetime.datetime.today() - datetime.timedelta(days=1)
last_date_fmt = datetime.datetime.strftime(yday_date,'%Y%m%d')
yday_mnth = last_date_fmt[:6]
reqItems = "https://xyz/prefix=dcm_accountclick_,yday_mnth"
print(reqItems.replace(",yday_mnth",yday_mnth))
Output:
https://xyz/prefix=dcm_accountclick_201903
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.