Transform URL string into normal string in Python (%20 to space etc)
Each Answer to this Q is separated by one/two green lines.
Is there any way in Python to transform this %CE%B1%CE%BB%20
into this ??
which is its real representation?
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"))
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).
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .