在Python的Django框架中用流響應(yīng)生成CSV文件的教程
在Django里,流式響應(yīng)StreamingHttpResponse是個(gè)好東西,可以快速、節(jié)省內(nèi)存地產(chǎn)生一個(gè)大型文件。
目前項(xiàng)目里用于流式響應(yīng)的一個(gè)是Eventsource,用于改善跨系統(tǒng)通訊時(shí)用戶產(chǎn)生的慢速的感覺。這個(gè)不細(xì)說(shuō)了。
還有一個(gè)就是生成一個(gè)大的csv文件。
當(dāng)Django進(jìn)程處于gunicorn或者uwsgi等web容器中時(shí),如果響應(yīng)超過(guò)一定時(shí)間沒(méi)有返回,就會(huì)被web容器終止掉,雖然我們可以通過(guò)加長(zhǎng)web容器的超時(shí)時(shí)間來(lái)繞過(guò)這個(gè)問(wèn)題,但是畢竟還是治標(biāo)不治本。要根本上解決這個(gè)問(wèn)題,Python的生成器、Django框架提供的StreamingHttpResponse這個(gè)流式響應(yīng)很有幫助
而在csv中,中文的處理也至關(guān)重要,要保證用excel打開csv不亂碼什么的。。為了節(jié)約空間,我就把所有代碼貼到一起了。。實(shí)際使用按照項(xiàng)目的規(guī)劃放置哈
上代碼:
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)
from django.views.generic import View
from django.http.response import StreamingHttpResponse
class ExampleView(View):
headers=['一些','表頭']
def get(self,request):
result = [['第一行','數(shù)據(jù)1'],
['第二行','數(shù)據(jù)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
- Django的HttpRequest和HttpResponse對(duì)象詳解
- Django使用HttpResponse返回圖片并顯示的方法
- Django使用httpresponse返回用戶頭像實(shí)例代碼
- django rest framework之請(qǐng)求與響應(yīng)(詳解)
- django從請(qǐng)求到響應(yīng)的過(guò)程深入講解
- 從請(qǐng)求到響應(yīng)過(guò)程中django都做了哪些處理
- Django框架的使用教程路由請(qǐng)求響應(yīng)的方法
- Django 中使用流響應(yīng)處理視頻的方法
- 詳解從Django Rest Framework響應(yīng)中刪除空字段
- Django 響應(yīng)數(shù)據(jù)response的返回源碼詳解
- django創(chuàng)建簡(jiǎn)單的頁(yè)面響應(yīng)實(shí)例教程
- Django框架HttpResponse對(duì)象用法實(shí)例分析
相關(guān)文章
Pandas聚合運(yùn)算和分組運(yùn)算的實(shí)現(xiàn)示例
這篇文章主要介紹了Pandas聚合運(yùn)算和分組運(yùn)算的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python實(shí)現(xiàn)讀取目錄所有文件的文件名并保存到txt文件代碼
這篇文章主要介紹了Python實(shí)現(xiàn)讀取目錄所有文件的文件名并保存到txt文件代碼,本文分別使用os.listdir和os.walk實(shí)現(xiàn)給出兩段實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-11-11
python中partial()基礎(chǔ)用法說(shuō)明
這篇文章主要給大家介紹了關(guān)于python中partial()基礎(chǔ)用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-12-12

