[请帮助我编写代码,以显示网站上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
有人可以帮助我理解如何正确编写。非常感谢
如果要在站点上显示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}}
呈现表。