如何在Python中取消引用URL引用的UTF-8字符串

时间:2009-11-09 11:20:38

标签: python

thestring = urllib.quote(thestring.encode('utf-8'))

这将对其进行编码。如何解码?

2 个答案:

答案 0 :(得分:6)

怎么样?
backtonormal = urllib.unquote(thestring)

答案 1 :(得分:2)

如果您要从utf-8解码字符串,您可以先将字符串转换为unicode,然后转换为您想要的任何其他编码(或将其保留为unicode),如下所示

unicodethestring = unicode(thestring, 'utf-8')
latin1thestring = unicodethestring.encode('latin-1','ignore')

'ignore'意味着如果遇到不在latin-1字符集中的字符,则忽略此字符。