From 9721b805f34069c1c0a068d43af71486133797ac Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 31 May 2019 18:20:24 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E8=A7=A3=E5=86=B3csv=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=AF=BC=E5=87=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/parsers/csv.py | 2 ++ apps/common/renders/csv.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/common/parsers/csv.py b/apps/common/parsers/csv.py index 4cd626cef..d96e9d2ef 100644 --- a/apps/common/parsers/csv.py +++ b/apps/common/parsers/csv.py @@ -3,6 +3,7 @@ import json import chardet +import codecs import unicodecsv from rest_framework.parsers import BaseParser @@ -82,6 +83,7 @@ class JMSCSVParser(BaseParser): try: stream_data = stream.read() + stream_data = stream_data.strip(codecs.BOM_UTF8) detect_result = chardet.detect(stream_data) encoding = detect_result.get("encoding", "utf-8") binary = self._universal_newlines(stream_data) diff --git a/apps/common/renders/csv.py b/apps/common/renders/csv.py index 7e57a97e1..f917edd4c 100644 --- a/apps/common/renders/csv.py +++ b/apps/common/renders/csv.py @@ -2,6 +2,7 @@ # import unicodecsv +import codecs from datetime import datetime from six import BytesIO @@ -73,7 +74,8 @@ class JMSCSVRender(BaseRenderer): table = self._gen_table(data, header, labels) csv_buffer = BytesIO() - csv_writer = unicodecsv.writer(csv_buffer, encoding='utf-8-sig') + csv_buffer.write(codecs.BOM_UTF8) + csv_writer = unicodecsv.writer(csv_buffer, encoding='utf-8') for row in table: csv_writer.writerow(row)