Python 2.7.6 + unicode_literals - UnicodeDecodeError:“ascii"编解码器无法解码字节 [英] Python 2.7.6 + unicode_literals - UnicodeDecodeError: 'ascii' codec can't decode byte

查看:61
本文介绍了Python 2.7.6 + unicode_literals - UnicodeDecodeError:“ascii"编解码器无法解码字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印以下 unicode 字符串,但我收到了 UnicodeDecodeError: 'ascii' codec can't decode byte 错误.你能帮助形成这个查询,以便它可以正确打印 unicode 字符串吗?

<预><代码>>>>从 __future__ 导入 unicode_literals>>>ts='现在'>>>free_form_request='[EXID(이엑스아이디)] 위아래 (UP&DOWN) MV'>>>昵称='我'>>>打印('{ts}:从{nick}'.format(ts=ts,free_form_request=free_form_request.encode('utf-8'),nick=nick)请求的自由格式请求{free_form_request})回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>UnicodeDecodeError:ascii"编解码器无法解码位置 6 中的字节 0xec:序号不在范围内(128)

在此先非常感谢您!

解决方案

以下是构造此字符串时发生的情况:

'{ts}:从 {nick}'.format(ts=ts,free_form_request=free_form_request.encode('utf-8'),nick=nick) 请求的自由格式请求 {free_form_request}

  1. free_form_request 使用 utf-8 作为编码,将 encode-d 转换成字节串.这是有效的,因为 utf-8 可以表示 [EXID(이엑스아이디)] 위아래 (UP&DOWN) MV.
  2. 然而,格式字符串('{ts}: free form request {free_form_request} requests from {nick}')是一个unicode字符串(因为导入了<代码>从 __future__ 导入 unicode_literals).
  3. 您不能使用字节字符串作为unicode字符串的格式参数,因此Python尝试对1中创建的字节字符串进行decode. 创建一个 unicode 字符串(作为格式参数是有效的).
  4. Python 尝试使用默认编码(ascii)进行 decode-ing,但失败了,因为字节字符串是 utf-8 包含在 ascii 中没有意义的字节值的字节字符串.
  5. Python 抛出一个 UnicodeDecodeError.

请注意,虽然代码显然在这里做了一些事情,但这实际上不会在 Python 3 上抛出异常,而是会替换字节字符串的 repr(reprcode> 是一个 unicode 字符串).

<小时>

要解决您的问题,只需将 unicode 字符串传递给 format.

也就是说,不要执行第 1 步.将 free_form_request 编码为字节字符串:通过删除 .encode(...):

'{ts}:从 {nick}'.format( 请求的自由格式请求 {free_form_request}ts=ts,free_form_request=free_form_request,尼克=尼克)

<小时>

在评论中也请注意 Padraic Cunningham 的回答.

I'm trying to print the following unicode string but I'm receiving a UnicodeDecodeError: 'ascii' codec can't decode byte error. Can you please help form this query so it can print the unicode string properly?

>>> from __future__ import unicode_literals
>>> ts='now'
>>> free_form_request='[EXID(이엑스아이디)] 위아래 (UP&DOWN) MV'
>>> nick='me'

>>> print('{ts}: free form request {free_form_request} requested from {nick}'.format(ts=ts,free_form_request=free_form_request.encode('utf-8'),nick=nick))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position 6: ordinal not in range(128)

Thank you very much in advance!

解决方案

Here's what happen when you construct this string:

'{ts}: free form request {free_form_request} requested from {nick}'.format(ts=ts,free_form_request=free_form_request.encode('utf-8'),nick=nick)

  1. free_form_request is encode-d into a byte string using utf-8 as the encoding. This works because utf-8 can represent [EXID(이엑스아이디)] 위아래 (UP&DOWN) MV.
  2. However, the format string ('{ts}: free form request {free_form_request} requested from {nick}') is a unicode string (because of imported from __future__ import unicode_literals).
  3. You can't use byte strings as format arguments for a unicode string, so Python attempts to decode the byte string created in 1. to create a unicode string (which would be valid as an format argument).
  4. Python attempts the decode-ing using the default encoding, which is ascii, and fails, because the byte string is a utf-8 byte string that includes byte values that don't make sense in ascii.
  5. Python throws a UnicodeDecodeError.

Note that while the code is obviously doing something here, this would actually not throw an exception on Python 3, which would instead substitute the repr of the byte string (the repr being a unicode string).


To fix your issue, just pass unicode strings to format.

That is, don't do step 1. where you encoded free_form_request as a byte string: keep it as a unicode string by removing .encode(...):

'{ts}: free form request {free_form_request} requested from {nick}'.format(
    ts=ts, 
    free_form_request=free_form_request, 
    nick=nick)


Note Padraic Cunningham's answer in the comments as well.

这篇关于Python 2.7.6 + unicode_literals - UnicodeDecodeError:“ascii"编解码器无法解码字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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