這是flabbergasting和非常令人沮喪,請幫助。爲什麼可以解碼u' xe5'而不是' xe5'?
>>> a1 = '\xe5' # type <str>
>>> a2 = u'\xe5' # type <unicode>
>>> ord(a1)
229
>>> ord(a2)
229
>>> print a2.encode('utf-8')
å
>>> print a1.encode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
如果a1和a2具有相同的值,爲什麼不能同時編碼?
我必須使用一個外部API,它返回a1
表單上的unicode數據,這使得它無用。 Python顯然堅持認爲<str>
鍵入的字符串只能包含ASCII字符或拒絕編碼它們。它徹底打破了我的申請。
您是否嘗試過使用它編碼的字符集對其進行解碼? –
嗯......原來數據編碼爲latin-1。根據我能找到的任何技術細節,我無法合理化正確的猜測。我不知道Python或者終端或者其他什麼都決定使用latin-1。我只是做了一個幸運的猜測。 –
它沒有。它是通過它產生的任何方式進行編碼的。 –