【发布时间】:2017-08-03 11:23:51
【问题描述】:
我编写了一个 python 脚本,它读取 csv 文件并将其显示在网页上。该网页是一个简单的html。
我正在尝试以某种格式显示它。我通过stackoverflow搜索并发现了这个: Formatting output of CSV file in Python 这很有帮助。
但是,使用print 函数时它工作正常,但是当我尝试渲染到我的网页时,格式不一样,它也只显示一行结果而不是整个文件。
那么我怎样才能让它在我的网页上显示与它在 python shell 上显示的相同格式呢?
这是我目前所拥有的:
@app.route('/results/view')
def my_results():
with open(str(get_file(filename)), "r") as f:
content = csv.reader(f)
for row in content:
content =(('{:^15} {:^15} {:^20} {:^25}'.format(*row)))
return render_template("results.html",content=content)
if __name__ == '__main__':
app.debug = True #Uncomment to enable debugging
app.run() #Run the Server
结果.html
<!DOCTYPE html>
<html>
<head>
<title>My webpage</title>
</head>
<body>
<a>{{ content }}</a>
</body>
</html>
带有print 语句而不是'content=' 的结果(我想要的)
A B
blue Car
yellow Bike
green Boat
content = 得到的结果
A green B Boat
【问题讨论】:
-
为什么这被否决了?
标签: python html rendering webpage