【发布时间】:2015-07-15 13:12:53
【问题描述】:
我正在尝试设置使用 Python 创建的 HTML 页面的样式,但不知何故我没有得到它。
以下是我没有样式标签的代码
htmlfile = open(fruit+".html")
htmlfile.write("<html>\n")
htmlfile.write("<head>\n")
htmlfile.write("<title> homepage </title>\n")
htmlfile.write("</head>\n")
htmlfile.write("<body>\n")
htmlfile.write("<h1> <center> <b> {color:red;} Fruit-properties </b> </center> </h1>\n")
htmlfile.write("</body>\n")
有人知道如何在 python 中添加样式标签吗?
【问题讨论】:
-
....考虑使用合适的模板引擎? stackoverflow.com/questions/2955615/python-template-engine
-
我想过。但 html 只是大代码的一小部分。所以我希望在当前代码本身中添加样式标签。
-
htmlfile.write("
\n")<font color="red"> Fruit-properties </font> -
谢谢。它在红字处显示无效语法
-
那是因为你需要转义“red”这个词周围的双引号,否则 Python 会假定你的 HTML 字符串正好在那个词之前结束。您还可以对实际的 python 字符串使用单引号(即
')或三引号。查看docs on strings。