Django generates a CSV file

Source: Internet
Author: User

ImportCSV fromDjango.httpImportHttpResponse#Number of unruly passengers each year 1995-2005. In a real application#This would likely come from a database or some other back-end data store.Unruly_passengers = [146,184,235,200,226,251,299,273,281,304,203]defunruly_passengers_csv (Request):
#Create the HttpResponse object with the appropriate CSV header.Response = HttpResponse (mimetype='Text/csv') #告诉浏览器, the returned document is a CSV file response['content-disposition'] ='attachment; Filename=unruly.csv' #响应会有一个附加的content-disposition header, which contains the file name of the CSV file #Create The CSV writer using the HttpResponse as the "file."writer =Csv.writer (response) Writer.writerow ([' Year','Unruly Airline passengers']) #调用writer.writerow and pass it to an iterative object such as a list or a tuple that can be written to a row in a CSV file for(year, NUM)inchZip (Range (1995, 2006), unruly_passengers): Writer.writerow ([year, Num])returnResponse

The CSV module operates on objects that are similar to files, so you can use HttpResponse to replace

Django generates a CSV file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.