[英]Using Python Variable in HTML / .format()
I am having a really hard time getting the "marker" to show up.我很难让“标记”出现。 I do not know how to use
.format()
correctly to show the marker inside the string.我不知道如何正确使用
.format()
来显示字符串内的标记。
Does the variable need to be in a specific location in the string?变量是否需要位于字符串中的特定位置? Trying to get a grasp of this for the first time.
第一次尝试掌握这一点。 Sorry if I am asking basic questions.
对不起,如果我问的是基本问题。
Keep getting : """.format(marker) KeyError: 'font-family'
. Not sure where the problem is.不断得到:
""".format(marker) KeyError: 'font-family'
。不确定问题出在哪里。
marker = "AUniqueMarker"
# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """\
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 15">
<meta name=Originator content="Microsoft Word 15">
<link rel=File-List href="Law_files/filelist.xml">
<!--[if gte mso 9]><xml>
# (...)
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
</style>
<![endif]--><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026"/>
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1"/>
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-US style='tab-interval:.5in'>
{marker}
</body>
</html>
""".format(marker=marker)
You need to escape the presence of other curly braces ( {
and }
) in the string, otherwise the string will be misinterpreted by format
. 您需要转义字符串中其他花括号(
{
和}
)的出现,否则该字符串将被format
误解。
You have to repeat the characters to escape them. 您必须重复字符才能转义它们。 In the string you are calling
format
on, change the line 在您调用
format
的字符串中,更改行
{mso-style-name:"Table Normal";
to 至
{{mso-style-name:"Table Normal";
and similarly for the closing bracket. 和类似的结束括号。
You must double the {
and }
in your string, otherwise format
will try to interpret text between braces. 您必须将字符串中的
{
和}
加倍,否则format
会尝试解释大括号之间的文本。
html = """\
your html code
""".replace("{", "{{").replace("}", "}}").format(marker=marker)
EDIT : replace
will transform {marker}
into {{marker}}
, so it will not be interpreted by format
... 编辑:
replace
将{marker}
转换为{{marker}}
,因此它不会被format
解释...
If you don't want to manipulate the HTML code and add extra {} or %, python's Template<\/strong> string might be exactly what you're looking for.如果您不想操作 HTML 代码并添加额外的 {} 或 %,python 的模板<\/strong>字符串可能正是您要寻找的。 Let's take a look at an example:
我们来看一个例子:
from string import Template
t = Template('<body> $marker </body>')
t.substitute(marker='Hello World!')
'<body> Hello World! </body>'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.