问题描述
我正在尝试打印以下 unicode 字符串,但我收到了 UnicodeDecodeError: 'ascii' codec can't decode byte
错误.你能帮助形成这个查询,以便它可以正确打印 unicode 字符串吗?
在此先非常感谢您!
以下是构造此字符串时发生的情况:
'{ts}:从 {nick}'.format(ts=ts,free_form_request=free_form_request.encode('utf-8'),nick=nick) 请求的自由格式请求 {free_form_request}
free_form_request
使用utf-8
作为编码,将encode
-d 转换成字节串.这是有效的,因为utf-8
可以表示[EXID(이엑스아이디)] 위아래 (UP&DOWN) MV
.- 然而,格式字符串(
'{ts}: free form request {free_form_request} requests from {nick}'
)是一个unicode字符串(因为导入了从 __future__ 导入 unicode_literals). - 您不能使用字节字符串作为unicode字符串的格式参数,因此Python尝试对1中创建的字节字符串进行
decode
. 创建一个 unicode 字符串(作为格式参数是有效的). - Python 尝试使用默认编码(
ascii
)进行decode
-ing,但失败了,因为字节字符串是utf-8
包含在ascii
中没有意义的字节值的字节字符串. - Python 抛出一个
UnicodeDecodeError
.
请注意,虽然代码显然在这里做了一些事情,但这实际上不会在 Python 3 上抛出异常,而是会替换字节字符串的 repr
(repr
code> 是一个 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)
free_form_request
isencode
-d into a byte string usingutf-8
as the encoding. This works becauseutf-8
can represent[EXID(이엑스아이디)] 위아래 (UP&DOWN) MV
.- However, the format string (
'{ts}: free form request {free_form_request} requested from {nick}'
) is a unicode string (because of importedfrom __future__ import unicode_literals
). - 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). - Python attempts the
decode
-ing using the default encoding, which isascii
, and fails, because the byte string is autf-8
byte string that includes byte values that don't make sense inascii
. - 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"编解码器无法解码字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!