2012-07-08 118 views
2

蟒蛇3.X蟒蛇輸入的UnicodeDecodeError:

>>> a = input() 
hope 
>>> a 
'hope' 
>>> b = input() 
håpe 
>>> b 
'håpe' 
>>> c = input() 

start typing hå... delete using backspace... and change to hope 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 1: invalid continuation byte 
>>> 

的情況並不可怕,我的工作圍繞它,但覺得奇怪的是當刪除,字節搞的一團糟。有沒有其他人經歷過這個?

終端歷史表明,它認爲我進入h?ope

什麼想法?

在正在使用此腳本的腳本中,我會導入readline以提供命令行歷史記錄。

+0

適合我。 – 2012-07-08 21:20:43

+0

聽起來像一個破碎的終端,它是什麼終端? – bobince 2012-07-09 13:40:01

+0

標準一個,6-7歲的macbook,10.6.8,雖然我運行遮陽板。 – beoliver 2012-07-09 13:44:21

回答

5

它看起來像退格刪除最後字節而不是最後字符。是什麼最終情況是,你鍵入:

68 c3 a5 
h | å 

68 c3 6f 70 65 
h | |o |p |e 

在UTF-8,與第一位集(c3)一個字節表示下一個字節一定有它的第一位集以及(見Wikipedia's description)。

確保您的終端仿真器和readline瞭解您使用的是UTF-8。

+0

這當然有道理。至少我知道發生了什麼事! – beoliver 2012-07-08 21:30:43