Search code examples
pythonregexstringreplacetranslate

special character in the string in python


I generate a string as below which is a df expression.

However when I add another two variables , I receive a string with lot of slash characters in it. Its working fine when using print but not working when I use return string in the function for second scenario. Could you advise me.

Below is the one which is generated properly.

str =  "{0}{1}.selectExpr('*',{2})".format(df,filter_feature,retrieved_features)

return str

"ptf_overall1.filter(ptf_overall1.measurement_group == 'test').selectExpr('*','case when email_14days > 0 then 1 else 0 end as journey_email_been_sent_flag','case when opened_14days > 0 then 1 else 0 end as journey_opened_flag')"

However when I tried to add below two strings to the existing string, it generates a string with lot of slashes

print(group)

  .groupBy("country")

print(sum)

  .sum("email_14days")



str1 = "{0}{1}.selectExpr('*',{2}){3}{4}".format(df,filter_feature,retrieved_features,group,sum)



return str1
  
'ptf_overall1.filter(ptf_overall1.measurement_group == \'test\').selectExpr(\'*\',\'case when email_14days > 0 then 1 else 0 end as journey_email_been_sent_flag\',\'case when opened_14days > 0 then 1 else 0 end as journey_opened_flag\').groupBy("country").sum("email_14days")'

Expected output should be

"ptf_overall1.filter(ptf_overall1.measurement_group == 'test').selectExpr('*','case when email_14days > 0 then 1 else 0 end as journey_email_been_sent_flag','case when opened_14days > 0 then 1 else 0 end as journey_opened_flag').groupBy("country").sum("email_14days")"

I tried using replace, re, and translate. However not getting the expected output.


Solution

  • A work around I found is as below. However this looks very silly for me. Can anyone suggest a better solution here.

    query = print("{0}{1}.selectExpr('*',{2}){3}{4}".format(df,filter_feature,retrieved_features,group,sum))
      return  query
    
    ptf_overall1.filter(ptf_overall1.measurement_group == 'test').selectExpr('*','case when email_14days > 0 then 1 else 0 end as journey_email_been_sent_flag','case when opened_14days > 0 then 1 else 0 end as journey_opened_flag').groupBy("country").sum("email_14days")