程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django16: generating CSV files

編輯:Python

1、 Definition

        csv Files are comma separated values (Comma-Separated Values,CSV), Sometimes it is also called separated value , Because the split character is not a comma , Its files store tabular data in plain text ( Numbers and documents ).

notes : Can be used by common tabulation tools , Such as excel And so on .

2、python In the middle of csv file

        python Provides built-in Library -csv, You can operate directly through this library csv file

example :

import csv
with open(‘xx.csv’, ‘w’, newline=’’) as csvfile:
writer = csv.writer(csvfile)
writer.writerow([‘a’, ‘b’, ‘c’])

3、csv File download

         In the website , Implementation download csv, Pay attention to the following :

         Respond to Content-Type The type needs to be modified to text/csv. This tells the browser that the document is csv file , instead of html file .

         The response will get an additional Content-Disposition header , It includes csv The name of the document . It will be used by the browser to open “ Save as …” Dialog box .

example :

(a)views.py

def test_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;filename="test.csv"'
all_data = ['a', 'b', 'c', 'd']
writer = csv.writer(response)
writer.writerow(all_data)
return response

(b)urls.py

urlpatterns = [
path('admin/', admin.site.urls),
path('test_csv', views.test_csv),
]

example : Generate the csv file

(a)views.py

def make_page_csv(request):
page_num = request.GET.get('page', 1)
all_data = ['a', 'b', 'c', 'd', 'e']
# initialization paginator
paginator = Paginator(all_data, 2)
# initialization Specific page number page object
c_page = paginator.page(int(page_num))
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;filename="page-%s.csv"'%(page_num)
writer = csv.writer(response)
for b in c_page:
writer.writerow([b])
return response

(b)urls.py

urlpatterns = [
path('admin/', admin.site.urls),
path('test_page', views.test_page),
path('make_page_csv', views.make_page_csv),
]

(c)template/test_page.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Pagination </title>
</head>
<body>
<a href="/make_page_csv?page={
{ c_page.number }}"> Generate csv</a>
{% for p in c_page %}
<p>{
{ p }}</p>
{% endfor %}
{% if c_page.has_previous %}
<a href="/test_page?page={
{ c_page.previous_page_number }}"> The previous page </a>
{% else %}
The previous page
{% endif %}
{% for p_num in paginator.page_range %}
{% if p_num == c_page.number %}
{
{ p_num }}
{% else %}
<a href="/test_page?page={
{ p_num }}">{
{ p_num }}</a>
{% endif %}
{% endfor %}
{% if c_page.has_next %}
<a href="/test_page?page={
{ c_page.next_page_number }}"> The next page </a>
{% else %}
The next page
{% endif %}
</body>
</html>

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved