49

Is there any way in Python to transform this %CE%B1%CE%BB%20 into this αλ which is its real representation?

1
  • What do you mean by "real representation"? Commented Aug 1, 2012 at 22:17

3 Answers 3

101

For python 2:

>>> import urllib2
>>> print urllib2.unquote("%CE%B1%CE%BB%20")
αλ 

For python 3:

>>> from urllib.parse import unquote
>>> print(unquote("%CE%B1%CE%BB%20"))
αλ

And here's code that works in all versions:

try:
    from urllib import unquote
except ImportError:
    from urllib.parse import unquote

print(unquote("%CE%B1%CE%BB%20"))
Sign up to request clarification or add additional context in comments.

2 Comments

For Python 3: import urllib.request urllib.request.unquote(...)
urllib has now put unqote in the parse module from urllib.parse import unquote (git.ziirish.me/ziirish/burp-ui/issues/75)
11

There are two encodings in play here. Your string has first been encoded as UTF-8, then each byte has been percent-encoded.

To get the original string back you need to first unquote it, and then decode it:

>>> import urllib
>>> s = '%CE%B1%CE%BB%20'
>>> result = urllib.unquote(s).decode('utf8')
>>> print result
αλ 

Note that you need a Unicode enabled console in order to display the value (if you get an error with the print statement, try running it in IDLE).

2 Comments

thanks a lot! decode('utf8') was useless to me, though. unquote(s) did the job!
But it does. Just tried it to be on the safe side (IDLE, py2.7)
4

python 3 answer

import urllib 
urllib.parse.unquote('/El%20Ni%C3%B1o/')

'/El Niño/'

source

1 Comment

import urllib.parse not urllib

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.