Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mimic JS decodeURIComponent and unescape in python

I want to mimic the decodeURIComponent and unescape that we have in JS in Python.

For example I get this string:

%25D7%2590%25D7%2591%25D7%2592

in JS I can do this:

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

and get: אבג

I couldn't find any way to do it in Python, I'm using Tornado Web if it's relevant...

like image 808
Elon Salfati Avatar asked Dec 11 '17 08:12

Elon Salfati


1 Answers

There's urllib.parse.unquote in Python (or urlparse.unquote in Python 2) to do that.

But I don't understand why the string you've posted is urlencoded twice. Because a single urlencoded string can be decoded just using decodeURIComponent (without the unescape).

Anyway, the Python version for this would be:

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'))

For a single urlencoded string, you only need to use unquote once.

like image 79
xyres Avatar answered Oct 08 '22 14:10

xyres