Views / Django如何从文件cvs中读取文件并在页面上显示信息

问题描述 投票:0回答:1

[请帮助我编写代码,以显示网站上cvs文件中的某些值。这是我在Django项目中的views.py。在网站视图上,工作并显示文件中的所有信息。但是我需要通过搜索一些值来显示信息,并向我显示该值。

views.py

from django.http import HttpResponse





def index(response):
    with open('news_rss/data.csv', 'r+', encoding='windows-1251') as file:
        response = HttpResponse(file.readlines())

        for line in file:
            word = 'Contract'
            if word in line != -1:
                return (line)





    return response

有人可以帮助我理解如何正确编写。非常感谢

python django view cvs
1个回答
0
投票

如果要在站点上显示CSV文件中的某些值,请考虑使用pandas之类的库。您可以在[here

上阅读更多内容

一旦您只选择了要在网站上显示的数据,就可以使用以下方法将pandas数据框转换为HTML表。

csvfile = request.FILES['csv_file']
data = pd.read_csv(csvfile.name)
data_html = data.to_html()
context = {'loaded_data': data_html}
return render(request, "dataflow/table.html", context)

在HTML页面中,使用

{{loaded_data | safe}}

呈现表。

© www.soinside.com 2019 - 2024. All rights reserved.