python中字符串中的特殊字符

2024-10-02 02:44:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我生成一个字符串,如下所示,它是一个df表达式

但是,当我添加另外两个变量时,我会收到一个包含大量斜杠字符的字符串。在第二种情况下,使用print时工作正常,但在函数中使用return string时不工作。你能给我提个建议吗

下面是正确生成的一个

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')"

但是,当我尝试向现有字符串添加以下两个字符串时,它会生成一个带有大量斜杠的字符串

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")'

预期产出应为

"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")"

我试着使用replace、re和translate。但是没有得到预期的输出


Tags: 字符串emailasfilterelseendflagwhen
1条回答
网友
1楼 · 发布于 2024-10-02 02:44:52

我发现的一个工作环境如下。然而,对我来说,这看起来很傻。这里有谁能提出更好的解决方案吗

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")

相关问题 更多 >

    热门问题