Coderaemon
Coderaemon

Reputation: 3877

Converting url encoded string(utf-8) to string in python?

I have a url encoded token(utf-8)= "EC%2d2EC7" and I want to convert it to "EC-2EC7" i.e convert %2d to -.

>>> token = "EC%2d2EC7"
>>> token.encode("utf-8")
'EC%2d2EC7'

I also tried urllib.quote but same result. Is the problem that token is already in utf-8 so it can't convert? What can I do?
My python version: 2.7.10

Upvotes: 2

Views: 971

Answers (2)

Raulucco
Raulucco

Reputation: 3426

You are looking for unquote instead of decode.

urllib.unquote('EC%2d2EC7')

Upvotes: 1

midori
midori

Reputation: 4837

You can use urllib.unquote:

from urllib import unquote

print unquote("EC%2d2EC7")

Another way is to use requests.utils.unquote:

from requests.utils import unquote

print unquote("EC%2d2EC7")

Output:

EC-2EC7

Upvotes: 4

Related Questions