UnicodeDecodeError:'utf-8'编解码器无法解码位置14的字节0xb9:无效的起始字节 [英] UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 14: invalid start byte

查看:413
本文介绍了UnicodeDecodeError:'utf-8'编解码器无法解码位置14的字节0xb9:无效的起始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django REST进行文件上传测试.
Python3.6.2
Django1.11
djangorestframework == 3.6.4
Excel-OSX 15.38(170902)
OSX 10.12.6

I am doing a File upload test with Django REST.
Python3.6.2
Django1.11
djangorestframework==3.6.4
Excel-OSX 15.38(170902)
OSX 10.12.6

过去,使用普通的照片文件可以成功完成.
这次是来自网站的Excel文件.这是我从参考文献中获得的测试用例副本.

It used to be done successfully with ordinary photo files.
This time is Excel file from website. Here is my testcase copy from references.

 def test_upload_and_process_data_complete_case(self):
        from django.core.files import File
        from django.core.files.uploadedfile import SimpleUploadedFile
        from soken_web.apps.imported_files.models import ImportFile

        file = File(open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx'))
        uploaded_file = SimpleUploadedFile('new_image.xlsx', file.read(), content_type='multipart/form-data')

        data = {
            'attribute': {'author': 'Sigh'},
            'type': ImportFile.FileType.zipcode,
            'file': uploaded_file
        }
        response = self.client.post(reverse('api:import_file-list'), data, format='multipart')
        response.render()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)

像复制猫一样.除了这次,我从 https://www.mockaroo.com/下载了一个模拟文件.

Like a copy cat. Except this time I download a mock file from https://www.mockaroo.com/.

这是我执行file.read()

file
<File: /Users/el/Code/norak-cutter/soken/soken-web/soken_web/apps/zipcodes/complete.xlsx>
file.read()
Traceback (most recent call last):
  File "/Users/el/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/172.3968.37/PyCharm.app/Contents/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "/Users/el/.pyenv/versions/3.6.2/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 14: invalid start byte

确认:
1.我可以从网络浏览器上传文件
2.我可以打开该文件而没有任何警告消息.

Confirmations:
1. I can upload file from my web browser
2. I can open that file without any warning messages.

问题:
我有什么要担心的事吗?

Question:
Are there anything I forgot to concern?

参考文献:
如何测试二进制文件用django-rest-framework的测试客户端上传?
Django REST UnitTest没有提交文件

References:
How can I test binary file uploading with django-rest-framework's test client?
Django REST UnitTest No file was submitted

推荐答案

打开文件的默认模式是"r",表示非二进制读取. Python假设您的文件是文本(编码)文件,并尝试对内容进行解码.但这不是文本文件,而是二进制数据文件.

The default mode of opening files is "r" which means non-binary read. Python is assuming your file is a text (encoded) file and trying to decode the contents. But it isnt a text file - it's a binary data file.

更改:

open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx')

收件人:

open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx', 'rb')

它可能会起作用.

这篇关于UnicodeDecodeError:'utf-8'编解码器无法解码位置14的字节0xb9:无效的起始字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆