根据条件格式化字符串

时间:2019-01-06 05:24:20

标签: python-3.x

我正在尝试根据某些条件动态创建邮件正文模板。

body = """ New product has created by {user}
           This can be viewed by clicking here {link}
           Logs is here {link2}
       """

我需要根据一些条件来格式化该字符串,例如:

if 'user' in params:
    body.format(user='username')
if 'link' in params:
    body.format(link='new link')
if 'link2' in params:
    body.format(link2='new link2')

我知道我可以做到以下

body.format(user='username',link='new link',link2='new link2')

但是我必须根据上述条件来执行此操作。有什么办法可以在python中实现吗?

我找到了this,但没有找到  帮帮我。

谢谢。

4 个答案:

答案 0 :(得分:0)

是的,我说我要在评论中使用lambda,但是定义一个函数对于扩展性会更好。这是您的代码:

def format_template(*args):
    user = 'default'
    link = 'something'
    link2 = 'something2'
    if 'user' in args:
        user = 'username'
    elif 'link' in args:
        link = 'link'
    elif 'link2' in args:
        link2 = 'link2'
    body = f"""
    New product has created by {user}
    This can be viewed by clicking here {link}
    Logs is here {link2}
    """
    return body

答案 1 :(得分:0)

这样的事情怎么办?

params.setdefault('user', 'unknown user')
params.setdefault('link', 'unknown link')
params.setdefault('link2', 'unknown log link')
body = """ New product has created by {user}
       This can be viewed by clicking here {link}
       Logs is here {link2}
       """.format(**params)

当然,您可以将默认值(“未知用户”等)更改为您认为合适的任何值。如果不允许您修改参数,则也可以使用params2 = dict(params)然后对params2进行操作。

答案 2 :(得分:0)

您链接的问题出了什么问题?这似乎可行:

body = """ New product has created by {user}
           This can be viewed by clicking here {link}
           Logs is here {link2}
       """.format(user='username' if 'user' in params else '???',
                  link='new link' if 'link' in params else '???',
                  link2='new link2' if 'link2' in params else '???')

答案 3 :(得分:0)

您可以尝试这样。

  

params是一本字典,因此您可以迭代其项并进行替换。

>>> def format(body, **params):
...     for key, value in params.items():
...         rep = "{" + key + "}"
...         if rep in body:
...             body = body.replace(rep, value)
...     return body
...
>>>
>>> body = """ New product has created by {user}
...            This can be viewed by clicking here {link}
...            Logs is here {link2}
...        """
>>>
>>> print(format(body, user='username'))
 New product has created by username
           This can be viewed by clicking here {link}
           Logs is here {link2}

>>> print(format(body, link='new link'))
 New product has created by {user}
           This can be viewed by clicking here new link
           Logs is here {link2}

>>> print(format(body, link2='new link2'))
 New product has created by {user}
           This can be viewed by clicking here {link}
           Logs is here new link2

>>>
>>> print(format(body, link2='new link2', link="new link"))
 New product has created by {user}
           This can be viewed by clicking here new link
           Logs is here new link2

>>> print(format(body, link2='new link2', link="new link", user="username"))
 New product has created by username
           This can be viewed by clicking here new link
           Logs is here new link2

>>>