【问题标题】:how i can import a html file from python [closed]我如何从 python 导入 html 文件 [关闭]
【发布时间】:2019-08-09 01:23:43
【问题描述】:

我想在python中导入一个html文件,以便在python代码中不写html

我尝试读取 html 文件并将其内容放入变量中,但它不读取 css 内容

html = open("templates/email.html","r")
msg.add_alternative(html.read(),subtype="html")        
html.close()

例子:

import 'file.html' as html
msg.add_alternative(html,subtype="html") 

【问题讨论】:

  • 你想在这里完成什么?不清楚你在问什么。
  • 我想导入 html 文件链接一个外部文件并把它放在它说“html.read()”的地方
  • 您已将此标记为烧瓶。如果您想向模板添加变量,那么您已经完全偏离了方向,您需要遵循一些烧瓶教程。 Jinja2 是默认的模板语言

标签: python html flask smtp


【解决方案1】:

这将打开并读取将其放入列表变量的文件。

        fileName = "filepath of the html I want to read"
        htmlText = file.text.readlines(fileName)

如果你想在 htmlText 变量中(在 HTML 中)找到某个元素。我会建议一个 for 循环和一个子字符串查找方法,如下所示。

        for i in range(0, len(htmlText):
           line=htmlText[i]
           if (line.find('some element inside HTML') != -1):
                print("I found it")
                # some other action 

【讨论】:

    【解决方案2】:

    我有

    from flask import render_template
    html = render_template('email.html');
    msg.add_alternative(html,subtype="html")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2013-10-23
      • 1970-01-01
      相关资源
      最近更新 更多