Chinese character export to csv file open by excel can't display correctly

15 views
Skip to first unread message

gyl...@gmail.com

unread,
Jul 25, 2016, 1:58:04 AM7/25/16
to airbnb_caravel
Chinese character export to csv file open by excel can't display correctly, I find solution is add the byte-order mark in front of the file, I eidt the views.py:

elif request.args.get("csv") == "true":
            payload = "\xef\xbb\xbf" + obj.get_csv()
            return Response(
                payload,
                status=200,
                headers=generate_download_headers("csv"),
                mimetype="application/csv")

it always report error information:"

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 4: ordinal not in range(128)"

How to resolve this problem?

Ray Xu

unread,
Jul 29, 2016, 4:12:39 AM7/29/16
to airbnb_caravel
just change the default encoding whne met the issue:
import sys reload(sys) sys.setdefaultencoding('utf-8')
Reply all
Reply to author
Forward
0 new messages