Python 如何解码以下字符串

Python 如何解码以下字符串,python,lxml,decode,Python,Lxml,Decode,我试图解码下面的字符串,但得到了一个错误 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

我试图解码下面的字符串,但得到了一个错误

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'
项[1]。文本=₨ 18500英镑
报告(项目[1]。文本)=u'\u20a8 18500'

您似乎正在尝试解码已解码的(Unicode)字符串。所以,放下
.decode('utf-8')
,它应该可以工作。除非“decode”是指其他内容(可能您希望将字符串编码为特定的编码)。

似乎您正在尝试解码已解码(Unicode)的字符串。所以,放下
.decode('utf-8')
,它应该可以工作。除非,您的“decode”是指其他内容(可能您希望将字符串编码为特定的编码)。

您调用了
decode
,但您的错误是引用
encode
,这表明您的字符串以Unicode开头,而不是bytestring
decode
用于从bytestring转换为Unicode,
encode
则相反。

您调用了
decode
,但您的错误是引用了
encode
,这表明您的字符串是Unicode,而不是bytestring
decode
用于从ByTestRing转换为Unicode,
encode
用于相反的方式。

请发布
项[1]的值。text
这样我们可以帮助您。相关的
repr(项[1].text)
可能更有用。(编辑您的问题,而不是在评论中提供信息)嗯,我的意思是向我们展示
repr(items[1].text)
,而不是在您的代码中使用它。它不会抛出该错误,但会非常难看。请发布
项[1]的值。text
以便我们可以帮助您。相关
报告(项[1]。text)
可能会更有用。(编辑您的问题,而不是在评论中提供信息)嗯,我的意思是向我们展示
repr(items[1].text)
,而不是在您的代码中使用它。它不会抛出那个错误,但会非常难看。python3让这更清楚,在这里你有
bytes
方法decode,和
str
方法encode。对不起,伙计们,我看它完全相反python3让这更清楚,你有
bytes
方法decode,还有
str
方法encode。对不起,伙计们,我的看法完全相反