Analysis of Implementation Method of python django Downloading Large csv File

  • 2021-07-24 11:07:30
  • OfStack

This article describes the example of python django download large csv file implementation. Share it for your reference, as follows:

To take over other people's projects, the first point to be optimized is to export the functions of csv, and to support more data export. It has been implemented with php before, and just write it directly to php://output. What should django do? As follows:

With the help of StreamingHttpResponse of django and generator of python


def outputCSV(rows, fname="output.csv", headers=None):
  def getContent(fileObj):
    fileObj.seek(0)
    data = fileObj.read()
    fileObj.seek(0)
    fileObj.truncate()
    return data
  def genCSV(rows, headers):
    #  Prepare output 
    output = cStringIO.StringIO()
    #  Write BOM
    output.write(bytearray([0xFF, 0xFE]))
    if headers != None and isinstance(headers, list):
      headers = codecs.encode("\t".join(headers) + "\n", "utf-16le")
      output.write(headers)
      yield getContent(output)
    for row in rows:
      rowData = codecs.encode("\t".join(row) + "\n", "utf-16le")
      output.write(rowData)
      yield getContent(output) # Because StreamingHttpResponse Need 1 A Iterator
    output.close()
  resp = StreamingHttpResponse(genCSV(rows, headers))
  resp["Content-Type"] = "application/vnd.ms-excel; charset=utf-16le"
  resp["Content-Type"] = "application/octet-stream"
  resp["Content-Disposition"] = "attachment;filename=" + fname
  resp["Content-Transfer-Encoding"] = "binary"
  return resp

Suppose the code for traversing the result set is as follows:


headers = ["col1", "col2", ..., "coln"]
def genRows():
      for obj in objList:
        yield [obj.col1, obj.col2, ...obj.coln]   
# Call like this , Return response
return outputCSV(genRows(), "file.csv", headers)

Some people may ask, why not use csv. writer that comes with python? Because the generated csv compatibility is not very good ah, about the compatibility of csv, you can see the previous article to avoid UTF-8 csv file opening Chinese garbled method.

Reference: http://stackoverflow.com/questions/5146539/streaming-a-csv-file-in-django

I hope this article is helpful to the Python programming based on Django framework.


Related articles: