Processing a Django UploadedFile as UTF-8 with universal newlines

前端 未结 3 1824
梦谈多话
梦谈多话 2020-12-31 08:51

In my django application, I provide a form that allows users to upload a file. The file can be in a variety of formats (Excel, CSV), come from a variety of platforms (Mac, L

3条回答
  •  一个人的身影
    2020-12-31 09:37

    As mentioned above, the code snippet I provided was in fact working as intended - the problem was with my terminal, and not with python encoding.

    If your view needs to access a UTF-8 UploadedFile, you can just use utf8_file = codecs.EncodedFile(request.FILES['file_field'],"utf-8") to open a file object in the correct encoding.

    I also noticed that, at least for InMemoryUploadedFiles, opening the file through the codecs.EncodedFile wrapper does NOT reset the seek() position of the file descriptor. To return to the beginning of the file (again, this may be InMemoryUploadedFile specific) I just used request.FILES['file_field'].open() to send the seek() position back to 0.

提交回复
热议问题