如何在Python中解码URL编码的UTF-8字符串

2 投票
2 回答
1350 浏览
提问于 2025-04-15 15:48
thestring = urllib.quote(thestring.encode('utf-8'))

这个会把它编码。那么,怎么解码呢?

2 个回答

2

如果你想把一个utf-8编码的字符串解码,你可以先把这个字符串转换成unicode,然后再转换成你想要的其他编码(或者保持在unicode格式),可以这样做:

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

这里的'ignore'意思是,如果你遇到一个不在latin-1字符集里的字符,你就忽略这个字符。

6

那关于这个呢

backtonormal = urllib.unquote(thestring)

撰写回答