2

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

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

This is it, thanks. What's the difference between unquote and quote ?
quote replaces special characters using the %xx escape, unquote changes it back
1

You are looking for unquote instead of decode.

urllib.unquote('EC%2d2EC7')

Comments

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.