简体   繁体   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?

I am new to Django and Python and was wondering how I could read a tab dilimited file into a html table by modifying my views.py and then returning the separate columns as a variable and returning that variable through params and then changing my template.html page. 我是Django和Python的新手,想知道如何通过修改views.py然后将单独的列作为变量返回并通过params返回该变量,然后更改template.html来将制表符分隔文件读入html表中。页。

so for example 例如

 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)

something of this sort any help is greatly appreciated 这种东西的任何帮助是极大的赞赏

My first recommendation is to use with to open files: 我的第一个建议是使用with打开文件:

with open('filename.ext', 'mode') as f:

Using with automagically closes your file for you so you don't have to explicitly do so :) with使用会自动为您关闭文件,因此您不必显式地这样做:)

Second, please visit: http://docs.python.org/3.3/library/csv.html#examples for a great explanation from the source. 其次,请访问: http : //docs.python.org/3.3/library/csv.html#examples ,以获取源代码的详尽解释。

The first example demos how to read your CSV file! 第一个示例演示了如何读取CSV文件!

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

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