0

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

Naveen
  • 1
  • 1
  • 1
    It's not clear what you are asking. The body of your question is focused on the "req_url += i" line, but the title suggest problems elsewhere. What is the exact problem? – bart cubrich Apr 01 '19 at 18:51
  • Naveen welcome to Stackoverflow. As bart suggested, it's not clear the purpose of your question. I will try to make a guess. If you want to load a config file with the structure key=value, and replace a specific key with a specific value: parse the file to a dictionary, and use the dictionary to locate the desired key, then replace the value on the key with your desired value.Take a look at this example: https://stackoverflow.com/questions/9161439/parse-key-value-pairs-in-a-text-file – dantebarba Apr 01 '19 at 18:55
  • `req_url = req_url.replace("yday_mnth", "201903")` – 001 Apr 01 '19 at 18:56
  • hi johnny thanks actually here i am tring to generate 201903 dynamically which i edited now – Naveen Apr 01 '19 at 19:11

1 Answers1

0

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
glhr
  • 4,439
  • 1
  • 15
  • 26
  • thanks glhr however here i dont want to replace exactly with yday_mnth bcoz if the requirement changes to another value then again i need to hard code so here i am trying to be more generic. – Naveen Apr 09 '19 at 14:06
  • @Naveen nothing is hard-coded here... the value of `yday_mnth` is created dynamically, based on the current date. So the code will have a different output depending on the current date. Can you explain what you're trying to achieve exactly? – glhr Apr 09 '19 at 18:53