我試圖打印下面的unicode字符串,但我收到了UnicodeDecodeError: 'ascii' codec can't decode byte
錯誤。你能幫助形成這個查詢,以便它可以正確打印unicode字符串嗎?Python 2.7.6 + unicode_literals - UnicodeDecodeError:'ascii'編解碼器無法解碼字節
>>> 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)
非常感謝您提前!
刪除編碼 –
嘗試[忽略錯誤](https://docs.python.org/2/library/stdtypes.html#str.encode):'free_form_request.encode('utf-8',errors = 'ignore')' –
@PeterWood這將不起作用,當字符串在編碼後解碼時會發生問題。請注意,由於unicode_literals導入,該字符串已經是unicode。 –