Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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/1/angular/31.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 在循环中使用模板安全替换函数_Python_String - Fatal编程技术网

Python 在循环中使用模板安全替换函数

Python 在循环中使用模板安全替换函数,python,string,Python,String,我有一个简单的代码: html_string = '''<html lang="en-US"> '<head> <title>My Python articles</title> </head> <body>''' for i in range(2): html_string += '''

我有一个简单的代码:

  html_string = '''<html lang="en-US">
            '<head> 
                <title>My Python articles</title>
            </head>
            <body>'''
    for i in range(2):
        html_string += '''
                <p>
                    <span style="white-space: pre-line">$''' + str(i) + '''</span>
                </p>'''

    html_string += '''</body>
        </html>'''

    html_template = Template(html_string)

    output_dir = "./html/"
    output_path = os.path.join(output_dir, 'my_page.html')
    with io.open(output_path, 'w+', encoding='UTF-8', errors='replace') as html_output:
        for i in range(2):
            html_output.write(html_template.safe_substitute(i="Hallo"))
            html_output.truncate()

$0
$1
只需要存在一次,并且必须用单词Hello替换它们。稍后,我将用不同的输入替换
$0
$1

模板字符串有以下关于替换标识符的说明:

默认情况下,“标识符”仅限于以下划线或ASCII字母开头的任何不区分大小写的ASCII字母数字字符串(包括下划线)

像“$0”和“$1”这样的标识符不满足此条件,因为它们以ASCII数字开头

在“$”和这样的数字之间插入一个字母应该可以:

html_string = '''<html lang="en-US">
            '<head>
                <title>My Python articles</title>
            </head>
            <body>'''

# Make substitution identifiers like "$Ti"
for i in range(2):
    html_string += ''' 
            <p>
                <span style="white-space: pre-line">$T''' + str(i) + '''</span>
            </p>'''

html_string += '''</body>
    </html>'''

html_template = Template(html_string)

# Map identifiers to values
mapping = {'T' + str(i): 'Hello' for i in range(2)}

output_dir = "./html/"
output_path = os.path.join(output_dir, 'my_page.html')

with open(output_path, 'w+', encoding='UTF-8', errors='replace') as html_output:
    html_output.write(html_template.safe_substitute(mapping))
    html_output.truncate()
html\u字符串=“”
'
我的Python文章
'''
#将替换标识符设置为“$Ti”
对于范围(2)中的i:
html_字符串+='''

$T'''+str(i)+''

'' html_字符串+=''' ''' html\u模板=模板(html\u字符串) #将标识符映射到值 映射={'T'+str(i):'Hello'表示范围(2)内的i} output_dir=“/html/” output\u path=os.path.join(output\u dir,'my\u page.html') 打开(输出路径为“w+”,编码为“UTF-8”,错误为“替换”)作为html\U输出: html_output.write(html_模板.safe_替换(映射)) html_output.truncate()
html_string = '''<html lang="en-US">
            '<head>
                <title>My Python articles</title>
            </head>
            <body>'''

# Make substitution identifiers like "$Ti"
for i in range(2):
    html_string += ''' 
            <p>
                <span style="white-space: pre-line">$T''' + str(i) + '''</span>
            </p>'''

html_string += '''</body>
    </html>'''

html_template = Template(html_string)

# Map identifiers to values
mapping = {'T' + str(i): 'Hello' for i in range(2)}

output_dir = "./html/"
output_path = os.path.join(output_dir, 'my_page.html')

with open(output_path, 'w+', encoding='UTF-8', errors='replace') as html_output:
    html_output.write(html_template.safe_substitute(mapping))
    html_output.truncate()