'ascii'编解码器无法编码...在上传csv文件时

时间:2017-12-22 13:49:01

标签: python django csv unicode

我上课时将带有假期的csv文件上传到我的fullcalendar。它看起来像这样:

class UploadVacationsView(APIView):
    def put(self, request, *args, **kwargs):
        try:
            # check file type
            mime = MimeTypes()
            url = urllib.pathname2url(request.FILES['file']._name)
            mime_type = mime.guess_type(url)
            if 'text/csv' not in mime_type:
                raise APIException(code=400, detail='File type must be CSV')
            vacations_list =[]
            csv_file = StringIO(request.data.get('file', None).read().decode('utf-8'))
            user_tz = pytz.timezone(request.user.common_settings.time_zone)
            schedule_file = ScheduleFile.objects.create(user=request.user)
            instance_hebcal = HebcalService()
            events = instance_hebcal.process_csv(csv_file, user_tz)
        ...

在另一个类中,我有一个适用于csv文件的方法:

class HebcalService(...):
    def process_csv(self, csv_file, user_tz):
        events = []
        csv_input = csv.reader(csv_file.readlines(), dialect=csv.excel)
        curr_row = 1
        start_date = None
        end_date = None
        start_name = None
        holiday_name = ''
        last_event = {'subject': '',
                     'date': '',
                     }

        for row in list(csv_input)[1:]:
            subject, date, time, _, _, _, _ = row[:7]
            curr_row += 1
            row = [unicode(cell.strip(), 'utf-8') for cell in row]

            if 'lighting' in subject and not start_date:
                start_date = user_tz.localize(format_datetime(date, time))
                if date == last_event['date']:
                    start_name = last_event['subject']

使用英语假日的名字时一切都很好但是当我遇到希伯来名字时它会出错:

Traceback (most recent call last):
  File "/home/stas/work/vacation/vmode/apps/marketplaces/base/api/views.py", line 47, in put
    events = instance_hebcal.process_csv(csv_file, user_tz)
  File "/home/stas/work/vacation/vmode/apps/marketplaces/base/services/hebcal.py", line 106, in process_csv
    for row in list(csv_input)[1:]:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 19-23: ordinal not in range(128)

我已经阅读过将所有字符串设置为unicode但不明白它在哪里获得默认ASCII编码,如何处理它并从csv文件中使用holiday_name保存字符串?

0 个答案:

没有答案