国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

在Python的Django框架中用流響應生成CSV文件的教程

2019-11-25 17:34:18
字體:
來源:轉載
供稿:網友

在Django里,流式響應StreamingHttpResponse是個好東西,可以快速、節省內存地產生一個大型文件。

目前項目里用于流式響應的一個是Eventsource,用于改善跨系統通訊時用戶產生的慢速的感覺。這個不細說了。

還有一個就是生成一個大的csv文件。

當Django進程處于gunicorn或者uwsgi等web容器中時,如果響應超過一定時間沒有返回,就會被web容器終止掉,雖然我們可以通過加長web容器的超時時間來繞過這個問題,但是畢竟還是治標不治本。要根本上解決這個問題,Python的生成器、Django框架提供的StreamingHttpResponse這個流式響應很有幫助

而在csv中,中文的處理也至關重要,要保證用excel打開csv不亂碼什么的。。為了節約空間,我就把所有代碼貼到一起了。。實際使用按照項目的規劃放置哈

上代碼:

from __future__ import absolute_importimport csvimport codecsimport cStringIOclass Echo(object):  def write(self, value):    return valueclass 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 Viewfrom django.http.response import StreamingHttpResponseclass ExampleView(View):  headers=['一些','表頭']  def get(self,request):    result = [['第一行','數據1'],         ['第二行','數據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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晋宁县| 辉南县| 东乡县| 射阳县| 临朐县| 门源| 玉屏| 甘洛县| 岳西县| 资讯 | 周口市| 隆回县| 扬州市| 遂川县| 贵州省| 和林格尔县| 昆山市| 丰顺县| 辛集市| 武宁县| 咸丰县| 年辖:市辖区| 新干县| 分宜县| 乌拉特后旗| 高邮市| 米泉市| 丹巴县| 双牌县| 宝应县| 西贡区| 富民县| 巴林左旗| 华池县| 宜宾市| 霞浦县| 余庆县| 榕江县| 左贡县| 通渭县| 平安县|