Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在html文件上打印Python代码_Python_Html_Printing_Echo - Fatal编程技术网

在html文件上打印Python代码

在html文件上打印Python代码,python,html,printing,echo,Python,Html,Printing,Echo,我这里有一个很难回答的问题,在php中,如果你想在html中打印一些东西,你可以这样做 <body> <div> <? echo 'Hi i am inside of a div'; ?> </div> </body> 在python中如何做到这一点?在一个简单的情况下,您可以使用。将占位符放入字符串中,并使用以实际文本替换占位符: html = """ <body> <div> {text} </d

我这里有一个很难回答的问题,在php中,如果你想在html中打印一些东西,你可以这样做

<body>
<div>
<?
echo 'Hi i am inside of a div';
?>
</div>
</body>


在python中如何做到这一点?

在一个简单的情况下,您可以使用。将占位符放入字符串中,并使用以实际文本替换占位符:

html = """
<body>
<div>
{text}
</div>
</body>
"""

print html.format(text='Hi i am inside of a div')
其中
index.html
包含:

<body>
    <div>
    {{ text }}
    </div>
</body>

{{text}}

如果我有复杂的模板怎么办?@Maks如果你有复杂的模板,你应该使用诸如flask、Django或pyramid之类的Web框架进行查看。我一直在搜索,似乎你可以使用,但似乎与我的工作不一样。你也可以使用
html.format(**d)
为多个占位符传递dict。
<body>
    <div>
    {{ text }}
    </div>
</body>