Tutorial on generating a CSV file with streaming responses in Python's Django framework

Source: Internet
Author: User
In Django, streaming response streaminghttpresponse is a good thing to produce a large file quickly and in a memory-saving manner.

One of the current projects for streaming response is EventSource, which is used to improve the user-generated sense of slowness when communicating across systems. This is not a detail.

Another is to generate a large CSV file.

When the Django process is in a web container such as Gunicorn or UWSGI, if the response is not returned for a certain amount of time, it will be terminated by the Web container, although we can bypass the problem by adding a longer web container timeout, but it is still a palliative. To solve this problem fundamentally, Python's generator, the streaminghttpresponse provided by the Django Framework, is a useful streaming response.

In the CSV, the Chinese processing is also very important, to ensure that Excel open CSV does not garbled anything. To save space, I pasted all the code together. The actual use is placed according to the plan of the project ha

On the code:

From __future__ import absolute_importimport csvimport codecsimport cstringioclass Echo (object): Def write (self, value): return valueclass unicodewriter: "" A CSV writer which would write rows to CSV file "F", which are encoded in the GI  VEN encoding. "" "Def __init__ (self, F, dialect=csv.excel, encoding=" Utf-8 ", **kwds): # Redirect output to a queue Self.queue = C Stringio.stringio () Self.writer = Csv.writer (Self.queue, Dialect=dialect, **kwds) Self.stream = f Self.encoder = Codecs.getincrementalencoder (Encoding) () def writerow (self, Row): Self.writer.writerow ([Handle_column (s) for s in row] # Fetch UTF-8 output from the queue ... data = Self.queue.getvalue () data = Data.decode ("Utf-8") # ... and R Eencode it into the target encoding data = Self.encoder.encode (data) # write to the target stream value = Self.st Ream.write (data) # empty queue self.queue.truncate (0) return value def writerows (self, rows): For row in rows : SelF.writerow (Row) 

From django.views.generic import viewfrom django.http.response import Streaminghttpresponseclass Exampleview (View):  headers=[' some ', ' table header ']  def get (self,request):    result = [[' First line ', ' Data 1 '],         [' Second row ', ' Data 2 ']]    echoer = Echo ()    writer = Unicodewriter (echoer)    def csv_itertor ():        yield codecs. Bom_utf8        yield Writer.writerow (self.headers) for        column in result:          yield writer.writerow (column)    Response = Streaminghttpresponse (      row for row in Csv_itertor ()),      content_type= "Text/csv;charset=utf-8")    response[' content-disposition '         = ' attachment;filename= ' example.csv '    return response
  • Related Article

    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.