2012-02-01 60 views
0

我試圖解碼下面的字符串,並得到一個錯誤。如何解碼以下字符串

item = lh.fromstring(items[1].text).text_content().strip().decode('utf-8') 

File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode 
return codecs.utf_8_decode(input, errors, True) 

UnicodeEncodeError: 'ascii' codec can't encode character u'\u20a8' in position 0: ordinal not in range(128) 

任何想法什麼是錯的?

items[1].text = <strong>₨ 18,500 </strong> 
repr(items[1].text) = u'\u20a8 18,500' 
+2

請發佈'items [1] .text'的值,以便我們幫助您。 – 2012-02-01 12:10:34

+1

相關http://www.fileformat.info/info/unicode/char/20a8/index.htm – 2012-02-01 12:12:12

+0

₨18,500 2012-02-01 12:12:22

回答

3

你調用了decode,但你的錯誤引用了encode的事實是你的字符串是以Unicode開始的,而不是字節串。 decode用於從字節轉換爲Unicode,encode用於其他方式。

+2

python3使這個更清晰,你有'方法解碼'字節,'方法編碼'str。 – steabert 2012-02-01 12:49:17

+0

對不起,我正在以完全相反的方式看它 – 2012-02-01 12:58:04

1

看來你試圖解碼一個已經解碼的(Unicode)字符串。所以,放下.decode('utf-8'),它應該工作。除非,你的意思是'解碼'的其他東西(也許你想編碼字符串到一些特定的編碼)。