UnicodeDecodeError:'utf-8'编解码器无法解码字节错误 [英] UnicodeDecodeError: 'utf-8' codec can't decode byte error

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

问题描述

我正在尝试从 urllib 获得回复,并将其解码为
到一个可读的格式。该文本是希伯来文,还包含诸如 { /

I'm trying to get a response from urllib and decode it to a readable format. The text is in Hebrew and also contains characters like { and /

首页编码是:

# -*- 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'

现在我试图使用以下方式解码:

Now I'm trying to decode it using:

 data = data.decode()

我收到以下错误:

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


推荐答案

你的问题是这不是UTF-8。您有 UTF-16 编码数据,解码为:

Your problem is that that is not UTF-8. You have UTF-16 encoded data, decode it as such:

>>> 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 code> Content-Type header 应该包含一个 charset 参数告诉你这个;如果响应是返回的 urllib.request 响应对象,则使用:

If you loaded this from a website with urllib.request, the Content-Type header should contain a charset parameter telling you this; if response is the returned urllib.request response object, then use:

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

如果没有设置 charset 参数,那么默认为UTF-8,这是JSON数据的相应默认值。

This defaults to UTF-8 when no charset parameter has been set, which is the appropriate default for JSON data.

或者,使用 请求要加载JSON响应,它会自动处理解码(包括特定于JSON响应的UTF-codec自动检测)。

Alternatively, use the requests library to load the JSON response, it handles decoding automatically (including UTF-codec autodetection specific to JSON responses).

还有一个注意事项:PEP 263源代码编解码器评论仅用于 来解释您的源代码,包括字符串文字。它与外部源(文件,网络数据等)的编码无关。

One further note: the PEP 263 source code codec comment is used only to interpret your source code, including string literals. It has nothing to do with encodings of external sources (files, network data, etc.).

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

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