簡體   English   中英

使用Django和Python讀取views.py中的文件並將制表符分隔的文件導出到HTML表中?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM