繁体   English   中英

通过Python打印出HTML格式的csv文件

[英]Print out a csv file in HTML through Python

基本上,这个想法是将注释存储在一个csv文件中,并将它们显示在同一页面上。 问题是我想把时间加粗,但找不到位置。 我认为主要问题在于HTML文件。

 from flask import Flask, redirect,render_template,request import csv from time import gmtime, strftime app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') def readFile(aFile): #read a file and return a list with open(aFile, 'r') as inFile: reader = csv.reader(inFile) aList = [row for row in reader] #I think the problem is here return aList def writeFile(aList, aFile): #write a list to file with open(aFile, 'w', newline='') as outFile: writer = csv.writer(outFile) print(aList) writer.writerows(aList) return @app.route('/comments') def comments(): #read the contacts list from file fileName = 'static\\\\comments.csv' commentList = readFile(fileName) return render_template('comments.html',commentList=commentList) @app.route('/addComment', methods = ['POST']) def addComment(): #read the contacts list from file fileName = 'static\\\\comments.csv' commentList = readFile(fileName) # add an entry to the skills list name = request.form[('name')] date = strftime("%a, %d %b %Y %X", gmtime()) #adds the current date and time comment = request.form[('comment')] newComment=[name,date,comment] commentList.append(newComment) #save the skills list to the file writeFile(commentList, fileName) return redirect('comments') 
 <ul class="comments"> {% for comments in commentList %}<br> <b><li>{% for names in comments %}</li> <!-- I think the problem is here--> <li>{{names}}</li></b> {% endfor %} {% endfor %} </ul> 

找到了解决方案。 该列表应如下所示:

  <ul class="comments"> {% for line in commentList %} <li> <b>{{ line[0] }}</b> <br> <b>{{ line[1] }}</b> <br> {{ line[2] }} </li><br> {% endfor %} </ul> 

W3Schools的HTML <li>列表 ,W3C的HTML文档中的列表

 <ol>
 <li>Coffee</li>
 <li>Tea</li>
 <li>Milk</li>
 </ol>

 <ul>
 <li>Coffee</li>
 <li>Tea</li>
 <li>Milk</li>
 </ul> 

对于<li> ... </li>行中的中断,可以使用br-tag。 <li> ... <br> ... <br> ... </li> br标签是单个的。 W3Schools的HTML <br>标记

<li> ... <br> ... <br> ... </li>

永远记住规则:在HTML内容中最好坚持[az] [0-9]。 因此在测试中没有花括号,也没有百分比符号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM