Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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/70.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 在使用Jinja呈现为HTML的文件中保留换行符_Python_Html_Flask_Jinja2 - Fatal编程技术网

Python 在使用Jinja呈现为HTML的文件中保留换行符

Python 在使用Jinja呈现为HTML的文件中保留换行符,python,html,flask,jinja2,Python,Html,Flask,Jinja2,我正在尝试打印网页中文件的内容。我想将文件中的每一行打印在单独的一行上,但是换行符丢失了。如何打印文件并保留换行符 @app.route('/users') def print_users(): v = open("users.txt","r").read().strip() # also tried: # v = open("users.txt","r").read().strip().split('\n') return render_template('we

我正在尝试打印网页中文件的内容。我想将文件中的每一行打印在单独的一行上,但是换行符丢失了。如何打印文件并保留换行符

@app.route('/users')
def print_users():
    v = open("users.txt","r").read().strip()
    # also tried:
    # v = open("users.txt","r").read().strip().split('\n')
    return render_template('web.html', v=v)
{{v|safe}
您可以使用:

v = open("users.txt","r").readlines()
v = [line.strip() for line in v]
然后在您的html中,类似这样的内容(但可以随意使用):


{v%中的行的%s}
{{line | safe}}
{%endfor%}

虽然其他答案给出了很好的技术,而且当前公认的解决方案也很有效,但我需要执行类似的任务(呈现文本电子邮件模板),但需要扩展此任务以保留文件中宏的处理,这让我创建了我认为最简单、最优雅的解决方案——使用render_template_string

def show_text_template(template_name):
    """
    Render a text template to screen.
    Replace newlines with <br> since rendering a template to html will lose the line breaks
    :param template_name: the text email template to show
    :return: the text email, rendered as a template
    """
    from flask import render_template_string

    txt_template = open(
        '{}/emails/{}{}'.format(app.config.root_path + '/' + app.template_folder, template_name, '.txt'),
        "r").readlines()

    return render_template_string('<br>'.join(txt_template))
def show_text_模板(模板名称):
"""
将文本模板呈现到屏幕。
将换行符替换为
,因为将模板呈现为html将丢失换行符 :param template_name:要显示的文本电子邮件模板 :return:文本电子邮件,呈现为模板 """ 从flask导入渲染模板字符串 txt_模板=打开( “{}/emails/{}{}.”格式(app.config.root_path+'/'+app.template_文件夹,template_名称,'.txt'), “r”)。读线() 返回渲染模板字符串(“
”.join(txt模板))
使用
readlines()
获取行列表,但如何分别打印每行?有代码示例吗?在html中添加for循环
def show_text_template(template_name):
    """
    Render a text template to screen.
    Replace newlines with <br> since rendering a template to html will lose the line breaks
    :param template_name: the text email template to show
    :return: the text email, rendered as a template
    """
    from flask import render_template_string

    txt_template = open(
        '{}/emails/{}{}'.format(app.config.root_path + '/' + app.template_folder, template_name, '.txt'),
        "r").readlines()

    return render_template_string('<br>'.join(txt_template))