用 Python 格式化 HTML

我想在 python 中格式化我的 html 代码。


我的 Python 文件是:


titles = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']

html_text = """<html>

<head>

    <style type="text/css">

        table { border-collapse: collapse;}

        td { text-align: center; border: 5px solid #ff0000; border-style: dashed; font-size: 30px; }

    </style>

</head>

<body>

    <table width="100%" height="100%" border="5px">

        <tr>

            <td>%s</td>

        </tr>

        <tr>

            <td>%s</td>

        </tr>

        <tr>

            <td>%s</td>

        </tr>

        <tr>

            <td>%s</td>

        </tr>

        <tr>

            <td>%s</td>

        </tr>

    </table>

</body>

</html> % (titles[0], titles[1], titles[2], titles[3], titles[4])"""


f = open('temp.html', 'w')

f.write(html_text)

f.close()

我想让那些 %s 成为 titles[0]、titles[1]、titles[2]、titles[3]、titles[4]。


我怎样才能做到?


慕盖茨4494581
浏览 327回答 3
3回答

明月笑刀无情

fstrings 是现在很酷的孩子们使用的东西。titles = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']html_text = f"""<html><head>&nbsp; &nbsp; <style type="text/css">&nbsp; &nbsp; &nbsp; &nbsp; table {{ border-collapse: collapse;}}&nbsp; &nbsp; &nbsp; &nbsp; td {{ text-align: center; border: 5px solid #ff0000; border-style: dashed; font-size: 30px; }}&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <table width="100%" height="100%" border="5px">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{titles[0]}</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{titles[1]}</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{titles[2]}</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{titles[3]}</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{titles[4]}</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table></body></html>"""with open('temp.html', 'w') as f:&nbsp; &nbsp; f.write(html_text)您将变量放入{}文本中,并且您的样式必须使用 double 进行转义{{}}。试试看。此外,pythonic 写入文件的方式是使用上下文管理器。它处理关闭而不需要.close()打开的文件。

慕斯709654

您可以使用一些模板引擎将逻辑混合到模板中。jinja2示例:安装 pip install jinja22 那么代码将是:html_text = """<html><head>...</head><body>&nbsp; &nbsp; <table width="100%" height="100%" border="5px">&nbsp; &nbsp; &nbsp; &nbsp; {% for title in titles %}&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{title}}</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; </table></body></html>"""from jinja2 import Templatetitles = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']my_templ = Template(html_text)with open('temp.html', 'w') as f:&nbsp; &nbsp; f.write(my_templ.render(titles=titles))请注意,处理可变长度列表是灵活的。Web 框架中使用了模板引擎。

ibeautiful

您的格式字符串中有两个错误。第一个,正如 U9-Forward 所指出的,在这里:</html> % (titles[0], titles[1], titles[2], titles[3], titles[4])"""这%是一个插值运算符,因此它需要在字符串和数据之间进行:</html>""" % (titles[0], titles[1], titles[2], titles[3], titles[4])第二个错误,只有在你修复了那个错误后才明显,在这里:<table width="100%" height="100%" border="5px">当您使用%运算符时,该字符%变得特殊,因此%s符合您的预期。但是当发生这种情况时,这"100%"是不合法的,因为正如错误消息告诉您的那样,它会将unsupported format character '"' (0x22) at index 237. 通过将光标放在字符串的开头并按向右箭头 237 次,您可以在一分钟内为自己找到这一点。在这种情况下,%您要留下的%必须加倍:<table width="100%%" height="100%%" border="5px">那给html_text = '''<html><head>&nbsp; &nbsp; <style type="text/css">&nbsp; &nbsp; &nbsp; &nbsp; table { border-collapse: collapse;}&nbsp; &nbsp; &nbsp; &nbsp; td { text-align: center; border: 5px solid #ff0000; border-style: dashed; font-size: 30px; }&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <table width="100%%" height="100%%" border="5px">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>%s</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>%s</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>%s</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>%s</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>%s</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table></body></html>''' % (titles[0], titles[1], titles[2], titles[3], titles[4])但这里的基本问题是 Python%字符串是一种格式化迷你语言,而 HTML 是一种格式化语言,因此像这样构造 HTML 意味着您同时使用两种语言进行编程。这涉及的双重思考让一些有经验的程序员感到兴奋,但我们其他人更乐意将我们的关注点分开并一次处理一种语言。而不是%-strings,考虑使用lxml来构造你的 HTML。有更多的学习曲线(通过优秀的教程缓解),但您的代码将更易于编写和维护,并lxml确保您的 HTML 没有语法错误。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python