[英]flask - python How to read a file and output to browser line by line and formats
I am developing a flask-python application. 我正在开发flask-python应用程序。 I want to read a file and out put the content with line-breaks and white spaces intact. 我想读取一个文件,然后将内容与换行符和空格保持完整。 Below is the code I have now. 以下是我现在拥有的代码。 But it outputs all the lines as a single stream without any line break. 但是它将所有行作为单个流输出,没有任何换行符。 What is the correct way to do it? 正确的方法是什么?
with open(anupath, 'r')as f:
content=""
for line in f:
content += line + '\n'
Your response is likely being treated as HTML by the browser, which collapses line breaks (among plenty of other things). 浏览器可能会将您的响应视为HTML,这会使换行符折叠(在许多其他事情中)。 If you want your file to be displayed as-is, set the parameter mimetype='text/plain'
on your flask Response
object before returning it: 如果mimetype='text/plain'
原样显示文件,请在返回烧瓶之前,在烧瓶Response
对象上设置参数mimetype='text/plain'
:
return Response(content, mimetype='text/plain')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.