How to generate a CSV file using a stream response in the Python Django framework
This article mainly introduces how to generate CSV files using stream responses in the Python Django framework. The author particularly talked about preventing Chinese Characters in CSV files from garbled characters. For more information, see
In Django, streaming response to StreamingHttpResponse is a good thing that can generate a large file in a fast and memory-saving manner.
Currently, Eventsource is used for stream response, which is used to improve the user's low speed during cross-system communication. This is not detailed.
Another is to generate a large csv file.
When the Django process is in a web Container such as gunicorn or uwsgi, if no response is returned after a certain period of time, it will be terminated by the web container, although we can extend the timeout time of web containers to bypass this problem, it is still a permanent cure. To fundamentally solve this problem, the StreamingHttpResponse stream response provided by the Python generator and Django framework is very helpful.
In csv, the processing of Chinese characters is also crucial. Make sure that the csv files in excel are not garbled .. To save space, I Will paste all the Code together .. Actual use is placed according to the project plan.
Code:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
From _ future _ import absolute_import Import csv Import codecs Import cStringIO Class Echo (object ): Def write (self, value ): Return value Class UnicodeWriter: """ A csv writer which will write rows to CSV file "f ", Which is encoded in the given encoding. """ Def _ init _ (self, f, dialect = csv. excel, encoding = "UTF-8", ** kwds ): # Redirect output to a queue Self. queue = cStringIO. 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 reencode it into the target encoding Data = self. encoder. encode (data) # Write to the target stream Value = self. stream. write (data) # Empty queue Self. queue. truncate (0) Return value Def writerows (self, rows ): For row in rows: Self. writerow (row) |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
From django. views. generic import View From django. http. response import StreamingHttpResponse Class ExampleView (View ): Headers = ['data', 'header'] Def get (self, request ): Result = [['first row', '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 |
Note <>: For more exciting tutorials, please pay attention to the help house programming