1
我希望在我的項目中使用unicode
而不是str
作爲所有字符串。我正在嘗試使用str.encode
方法,但無法從文檔中瞭解encode
方法確切地做什麼或期望作爲輸入。str.encode期望輸入什麼內容?
希臘小寫字母pi是U + 03C0,當用UTF-8編碼時是0xCF 0x80。我得到如下:
>>> s1 = '\xcf\x80'
>>> s1.encode('utf-8','ignore')
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
s1.encode('utf-8','ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcf in position 0: ordinal not in range(128)
我試了:
>>> s2='\x03\xc0'
>>> s2.encode('utf-8','ignore')
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
s2.encode('utf-8','ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 1: ordinal not in range(128)
是什麼encode
期望作爲輸入,以及爲何「忽略」選項不可忽視的錯誤?我嘗試'替換',也沒有掩蓋錯誤。