Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.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
如何使用Python 3将HTML文档中的{{var}替换为变量_Python_Html - Fatal编程技术网

如何使用Python 3将HTML文档中的{{var}替换为变量

如何使用Python 3将HTML文档中的{{var}替换为变量,python,html,Python,Html,如何将dict中的变量放入HTML文档 就像用Python在HTML文档中将{{variable}}替换为1一样 Python代码: def convert_html_to_python(变量={},代码=”): ##填充代码变量 某些_代码=”“ 我的第一个标题 我的第一段 {{var}} """ 将html转换为python({“var”:“1”},一些代码) 然后,Python脚本将HTML文档转换为: 我的第一个标题 我的第一段 1. 我希望它像模板一样。我不想使用Django或F

如何将dict中的变量放入HTML文档

就像用Python在HTML文档中将{{variable}}替换为1一样

Python代码:

def convert_html_to_python(变量={},代码=”):
##填充代码变量
某些_代码=”“
我的第一个标题
我的第一段

{{var}} """ 将html转换为python({“var”:“1”},一些代码)
然后,Python脚本将HTML文档转换为:


我的第一个标题
我的第一段

1.

我希望它像模板一样。我不想使用Django或Flask。

Flask和Django提供了解决方案。 您可以尝试像字符串一样读取HTML文件并对其进行更改

例如:

HTML代码

我的第一个标题
我的第一段

用于添加数据的函数:

def add_data(data, html_code):
    write_index = html_code.find('</body>') - 1
    html_code = html_code[:write_index] + '\n<h1>' + data + '</h1>\n' + html_code[write_index + 1:]
    print(html_code)
def添加数据(数据、html代码):
write_index=html_code.find(“”)-1
html_code=html_code[:write_index]+'\n'+数据+'\n'+html_code[write_index+1:]
打印(html_代码)
HTML现在:

我的第一个标题
我的第一段

~~我们插入的数据~~
Flask和Django提供了解决方案。 您可以尝试像字符串一样读取HTML文件并对其进行更改

例如:

HTML代码

我的第一个标题
我的第一段

用于添加数据的函数:

def add_data(data, html_code):
    write_index = html_code.find('</body>') - 1
    html_code = html_code[:write_index] + '\n<h1>' + data + '</h1>\n' + html_code[write_index + 1:]
    print(html_code)
def添加数据(数据、html代码):
write_index=html_code.find(“”)-1
html_code=html_code[:write_index]+'\n'+数据+'\n'+html_code[write_index+1:]
打印(html_代码)
HTML现在:

我的第一个标题
我的第一段

~~我们插入的数据~~
您可以在字符串上使用该方法

def convert_html_to_python(variables={}, code=""):
    for variable in variables:
        code = code.replace("{{"+str(variable)+"}}", variables[variable])
    return code

some_code = """
<!DOCTYPE html>
<html>

<body>
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
    {{var}}
</body>
</html>
"""
print(convert_html_to_python({"var":"1"}, some_code))
def convert_html_to_python(变量={},代码=”):
对于变量中的变量:
code=code.replace(“{{{”+str(variable)+“}}”),variables[variable])
返回码
某些_代码=”“
我的第一个标题
我的第一段

{{var}} """ 打印(将html转换为python({“var”:“1”},一些代码))
您可以在字符串上使用该方法

def convert_html_to_python(variables={}, code=""):
    for variable in variables:
        code = code.replace("{{"+str(variable)+"}}", variables[variable])
    return code

some_code = """
<!DOCTYPE html>
<html>

<body>
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
    {{var}}
</body>
</html>
"""
print(convert_html_to_python({"var":"1"}, some_code))
def convert_html_to_python(变量={},代码=”):
对于变量中的变量:
code=code.replace(“{{{”+str(variable)+“}}”),variables[variable])
返回码
某些_代码=”“
我的第一个标题
我的第一段

{{var}} """ 打印(将html转换为python({“var”:“1”},一些代码))
我不知道你到底需要做什么,vue我认为flask和django提供了一个html模板引擎。我不想使用框架。@Insaeminer你可以使用jinja2alone@KetZoomer好的,谢谢我不知道你到底需要做什么,vue我认为flask和django附带了一个html模板引擎。我不想使用框架。@InsaneMiner你可以使用jinja2alone@KetZoomer好的,谢谢