簡體   English   中英

UnicodeDecodeError: 'utf-8' 編解碼器無法解碼字節錯誤

[英]UnicodeDecodeError: 'utf-8' codec can't decode byte error

我正在嘗試從urllib獲取響應並將其解碼為可讀格式。 文本是希伯來語,還包含像{/

首頁編碼是:

# -*- coding: utf-8 -*-

原始字符串是:

b'\xff\xfe{\x00 \x00\r\x00\n\x00"\x00i\x00d\x00"\x00 \x00:\x00 \x00"\x001\x004\x000\x004\x008\x003\x000\x000\x006\x004\x006\x009\x006\x00"\x00,\x00\r\x00\n\x00"\x00t\x00i\x00t\x00l\x00e\x00"\x00 \x00:\x00 \x00"\x00\xe4\x05\xd9\x05\xe7\x05\xd5\x05\xd3\x05 \x00\xd4\x05\xe2\x05\xd5\x05\xe8\x05\xe3\x05 \x00\xd4\x05\xea\x05\xe8\x05\xe2\x05\xd4\x05 \x00\xd1\x05\xde\x05\xe8\x05\xd7\x05\xd1\x05 \x00"\x00,\x00\r\x00\n\x00"\x00d\x00a\x00t\x00a\x00"\x00 \x00:\x00 \x00[\x00]\x00\r\x00\n\x00}\x00\r\x00\n\x00\r\x00\n\x00'

現在我正在嘗試使用以下方法對其進行解碼:

 data = data.decode()

我收到以下錯誤:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

你的問題是那不是 UTF-8。 您有UTF-16編碼的數據,將其解碼為:

>>> data = b'\xff\xfe{\x00 \x00\r\x00\n\x00"\x00i\x00d\x00"\x00 \x00:\x00 \x00"\x001\x004\x000\x004\x008\x003\x000\x000\x006\x004\x006\x009\x006\x00"\x00,\x00\r\x00\n\x00"\x00t\x00i\x00t\x00l\x00e\x00"\x00 \x00:\x00 \x00"\x00\xe4\x05\xd9\x05\xe7\x05\xd5\x05\xd3\x05 \x00\xd4\x05\xe2\x05\xd5\x05\xe8\x05\xe3\x05 \x00\xd4\x05\xea\x05\xe8\x05\xe2\x05\xd4\x05 \x00\xd1\x05\xde\x05\xe8\x05\xd7\x05\xd1\x05 \x00"\x00,\x00\r\x00\n\x00"\x00d\x00a\x00t\x00a\x00"\x00 \x00:\x00 \x00[\x00]\x00\r\x00\n\x00}\x00\r\x00\n\x00\r\x00\n\x00'
>>> data.decode('utf16')
'{ \r\n"id" : "1404830064696",\r\n"title" : "פיקוד העורף התרעה במרחב ",\r\n"data" : []\r\n}\r\n\r\n'
>>> import json
>>> json.loads(data.decode('utf16'))
{'title': 'פיקוד העורף התרעה במרחב ', 'id': '1404830064696', 'data': []}

如果您從帶有urllib.request的網站加載它,則Content-Type標頭包含一個charset參數,告訴您這一點; 如果response是返回的urllib.request響應對象,則使用:

codec = response.info().get_content_charset('utf-8')

如果沒有設置charset參數,則默認為 UTF-8,這是 JSON 數據的適當默認值。

或者,使用requests加載 JSON 響應,它會自動處理解碼(包括特定於 JSON 響應的 UTF-codec 自動檢測)。

進一步說明: PEP 263 源代碼編解碼器注釋用於解釋您的源代碼,包括字符串文字。 它與外部源(文件、網絡數據等)的編碼無關。

我在使用Python 3.4 Django遇到了這個錯誤。 我試圖讓它django-rest-framework 一起使用

這是我修復錯誤UnicodeDecodeError: 'utf-8' codec can't decode byte error 的代碼

這是通過的測試:

import os
from os.path import join, dirname
import uuid
from rest_framework.test import APITestCase

class AttachmentTests(APITestCase):

    def setUp(self):
        self.base_dir = dirname(dirname(dirname(__file__)))

        self.image = join(self.base_dir, "source/test_in/aaron.jpeg")
        self.image_filename = os.path.split(self.image)[1]

    def test_create_image(self):
        id = str(uuid.uuid4())
        with open(self.image, 'rb') as data:
            # data = data.read()
            post_data = {
                'id': id,
                'filename': self.image_filename,
                'file': data
            }

            response = self.client.post("/api/admin/attachments/", post_data)

            self.assertEqual(response.status_code, 201)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM