Skip to content

Instantly share code, notes, and snippets.

@halink0803
Last active April 22, 2021 07:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halink0803/5cff58e2762d495c818f047e96eaabd5 to your computer and use it in GitHub Desktop.
Save halink0803/5cff58e2762d495c818f047e96eaabd5 to your computer and use it in GitHub Desktop.

This snippet export utf-8 data to csv file that Excel can read.

 import io
 import csv
 from flask import make_response
 
 
 @app.route('/export', methods='GET')
 def export():
    """
      Base on a couple of answer on Stack Overflow
    """
    si = io.BytesIO()
    cw = csv.writer(si, dialect='excel', encoding='utf-8-sig')
    cw.writerows(data)
    output = make_response(si.getvalue())
    output.headers["Content-Disposition"] = "attachment; filename=export.csv"
    output.headers["Content-type"] = "text/csv"
    return output
@pghole
Copy link

pghole commented Mar 24, 2020

How do you pass the "data" ?

@sirkordi
Copy link

csv.writer does not accept encoding as parameter :)

@serabutantekno
Copy link

csv.writer does not accept encoding as parameter :)

You're right. Do you know the solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment