From 49632241b6bbf87e0a5c27137b8d2362b7acffff Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 31 May 2019 17:40:57 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E5=AF=BC=E5=85=A5=E8=A7=A3=E6=9E=90?= =?UTF-8?q?csv=20coding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/parsers/csv.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/common/parsers/csv.py b/apps/common/parsers/csv.py index b536a0f73..4cd626cef 100644 --- a/apps/common/parsers/csv.py +++ b/apps/common/parsers/csv.py @@ -2,6 +2,7 @@ # import json +import chardet import unicodecsv from rest_framework.parsers import BaseParser @@ -73,7 +74,6 @@ class JMSCSVParser(BaseParser): def parse(self, stream, media_type=None, parser_context=None): parser_context = parser_context or {} - encoding = parser_context.get('encoding', 'utf-8') try: serializer = parser_context["view"].get_serializer() except Exception as e: @@ -82,6 +82,8 @@ class JMSCSVParser(BaseParser): try: stream_data = stream.read() + detect_result = chardet.detect(stream_data) + encoding = detect_result.get("encoding", "utf-8") binary = self._universal_newlines(stream_data) rows = self._gen_rows(binary, charset=encoding) @@ -97,5 +99,5 @@ class JMSCSVParser(BaseParser): data.append(row_data) return data except Exception as e: - logger.debug(e, exc_info=True) + logger.error(e, exc_info=True) raise ParseError('CSV parse error!')