在python中模仿JS decodeURIComponent和unescape

时间:2017-12-11 08:50:36

标签: javascript python-2.7

我想模仿我们在Python中的JS中的decodeURIComponent和unescape。

例如我得到这个字符串:

%25D7%2590%25D7%2591%25D7%2592
在JS中我可以这样做:

decodeURIComponent(unescape('%25D7%2590%25D7%2591%25D7%2592'))

并得到:     אבג

我在Python中找不到任何方法,如果它是相关的,我正在使用Tornado Web ......

1 个答案:

答案 0 :(得分:3)

Python中有urllib.parse.unquote(或Python 2中为urlparse.unquote)。

但我不明白为什么你发布的字符串是urlencoded 两次。因为只能使用decodeURIComponent(没有unescape)解码单个urlencoded字符串。

无论如何,这个Python的版本是:

from urllib.parse import unquote # For Python 2 use: from urlparse import unquote

# decode twice because the string has been encoded twice.
unquote(unquote('%25D7%2590%25D7%2591%25D7%2592'))

对于单个urlencoded字符串,您只需要使用unquote一次。

相关问题