2012-02-01 41 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'),它应该工作。除非,你的意思是'解码'的其他东西(也许你想编码字符串到一些特定的编码)。