【问题标题】:Insert Python variable into HTML code in Python script [duplicate]在Python脚本中将Python变量插入HTML代码[重复]
【发布时间】:2022-02-03 20:54:18
【问题描述】:

我有一个 python 脚本,它生成一些 HTML 代码以通过电子邮件发送。在脚本中有一个变量(tweet),我想将内容包含在电子邮件中。

这是为电子邮件生成 HTML 的代码:

msg.add_alternative("""\
<!DOCTYPE html>
<html>
    <body>
        <p>Hi Human,</p>
        <p>If you are seeing this, it means that you have received my email. Check out these images!</p>
        <p>Best,</p>
        <p>Simon</p>
        <img src="cid:image1" ><br>
    </body>
</html>
""", subtype='html')

我想将变量“tweet”添加到 HTML 代码的正文中,但不知道如何中断 HTML 并插入变量并继续 HTML 以完成。

有人可以帮忙吗?

【问题讨论】:

    标签: python html


    【解决方案1】:

    您有几个选项可以将变量添加到字符串中:

    1. 使用format -
    "data is {0}, I'm {1}".format(name, id)
    
    1. 使用f"text text {your variable}"

    我不确定你想在哪里添加你的变量,所以我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 1970-01-01
      • 2013-01-02
      • 2018-12-20
      • 2017-09-11
      • 1970-01-01
      • 2016-06-10
      • 2018-02-08
      • 2013-06-30
      相关资源
      最近更新 更多