Python 2.7.6+;unicode#u文字-UnicodeDecodeError:';ascii';编解码器可以';t解码字节
我正试图打印以下unicode字符串,但收到一个Python 2.7.6+;unicode#u文字-UnicodeDecodeError:';ascii';编解码器可以';t解码字节,python,python-2.7,unicode,Python,Python 2.7,Unicode,我正试图打印以下unicode字符串,但收到一个UnicodeDecodeError:“ascii”编解码器无法解码字节错误。您能否帮助形成此查询,以便它能够正确打印unicode字符串 >>> from __future__ import unicode_literals >>> ts='now' >>> free_form_request='[EXID(이엑스아이디)] 위아래 (UP&DOWN) MV' >>>
UnicodeDecodeError:“ascii”编解码器无法解码字节
错误。您能否帮助形成此查询,以便它能够正确打印unicode字符串
>>> 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)
>>从未来导入unicode文本
>>>现在
>>>自由形式请求='[EXID](이엑스아이디)] 위아래 (上下)MV'
>>>尼克:“我”
>>>打印({ts}:从{nick}请求的自由格式请求{free_form_request})。格式(ts=ts,free_form_request=free_form_request.encode('utf-8'),nick=nick))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
UnicodeDecodeError:“ascii”编解码器无法解码位置6:序号不在范围(128)中的字节0xec
非常感谢您!以下是构造此字符串时发生的情况:
'{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\u form\u request
是使用utf-8
作为编码,将编码为字节字符串。这是因为utf-8
可以表示[EXID](이엑스아이디)] 위아래 (上下)MV
{ts}:free form request{free\u form\u request}请求自{nick}
)是一个unicode字符串(因为从{uuuuuu future}导入的,导入unicode文本
)解码在1中创建的字节字符串。创建unicode字符串(作为格式参数有效)
ascii
)进行解码,但失败了,因为字节字符串是utf-8
字节字符串,其中包含在ascii
中没有意义的字节值
UnicodeDecodeError
repr
(repr
是unicode字符串)
要解决您的问题,只需将unicode字符串传递到
格式
也就是说,不要执行步骤1。将自由形式请求
编码为字节字符串:通过删除,将其保留为unicode字符串。encode(…)
:
请注意Padraic Cunningham在评论中的回答。以下是构造此字符串时发生的情况:
'{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\u form\u request
是使用utf-8
作为编码,将编码为字节字符串。这是因为utf-8
可以表示[EXID](이엑스아이디)] 위아래 (上下)MV
{ts}:free form request{free\u form\u request}请求自{nick}
)是一个unicode字符串(因为从{uuuuuu future}导入的,导入unicode文本
)解码在1中创建的字节字符串。创建unicode字符串(作为格式参数有效)
ascii
)进行解码,但失败了,因为字节字符串是utf-8
字节字符串,其中包含在ascii
中没有意义的字节值
UnicodeDecodeError
repr
(repr
是unicode字符串)
要解决您的问题,只需将unicode字符串传递到
格式
也就是说,不要执行步骤1。将自由形式请求
编码为字节字符串:通过删除,将其保留为unicode字符串。encode(…)
:
请注意Padraic Cunningham在评论中的回答。Try:
free\u form\u request.encode('utf-8',errors='ignore')
@PeterWood这行不通,当字符串在编码后解码时会出现问题。请注意,由于unicode\u文本的导入,字符串已经是unicode了。@thomasorzoah,我错过了。请尝试:free\u form\u request.encode('utf-8',errors='ignore'))
@PeterWood这行不通,当字符串在编码后解码时会出现问题。请注意,由于unicode\u文本
import,字符串已经是unicode了。@ThomasOrozco啊,我错过了。我在一个更大的函数中找到了原始问题的代码块。结果是我导入的某个东西重新写入了print函数,无法正确处理unicode字符串。不幸的是,我的解决方法是编码('ascii',errors='ignore'),删除unicode字符,然后处理它。@PrestonConnors然后您应该编码格式化字符串,而不是格式化参数。只需执行:print('…').format(…).encode('utf-8'))
虽然原始代码不会在Python 3上引发异常,但Python 3不会隐式地将字节字符串解码回Unicode(步骤3),所以打印的是字节字符串编码的repr
。要正确使用Python 3,仍然必须删除encode
。我在一个更大的函数中保留了原始问题中的代码块。结果是,我导入的某个东西重新写入了打印函数,无法正确处理unicode字符串。Unfo当然,我的解决方法是编码('ascii',errors='ignore')并删除unicode字符,然后处理它。@PrestonConnors然后您应该编码格式化字符串,而不是格式化参数。只需执行:print('…')。format(…)。encode('