[英]Using Django and Python to read in file in views.py and exporting the tab dilimited file to a HTML table?
我是Django和Python的新手,想知道如何通過修改views.py然后將單獨的列作為變量返回並通過params返回該變量,然后更改template.html來將制表符分隔文件讀入html表中。頁。
例如
def index(request):
myfile = open (filename.txt)
for row in myfile:
list = row.rstrip().split('\t')
params = {
"first" = list[0]
}
return render(request, 'index.html', params)
這種東西的任何幫助是極大的贊賞
我的第一個建議是使用with
打開文件:
with open('filename.ext', 'mode') as f:
with
使用會自動為您關閉文件,因此您不必顯式地這樣做:)
其次,請訪問: http : //docs.python.org/3.3/library/csv.html#examples ,以獲取源代碼的詳盡解釋。
第一個示例演示了如何讀取CSV文件!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.