2

I want to create a query string but without encoding the special character like @, ! or ?.

Here's my code:

payload = {"key": "value", "key2": "value2",
           "email": "[email protected]", "password": "myPassword54321!?"}
print(urllib.parse.urlencode(payload))  

Now I get as output this:

password=myPassword54321%21%3F&email=test%40hello3.ch

How can I make my output look like this:

password=myPassword54321!?&[email protected]

0

2 Answers 2

5

urllib.parse.unquote will replace the %xx character escapes with the characters they represent

from urllib.parse import urlencode, unquote

print(unquote(urlencode(payload)))
# key=value&key2=value2&[email protected]&password=myPassword54321!?
Sign up to request clarification or add additional context in comments.

Comments

3

If you don't want to use the encoding features of urlencode, you may as well not use it, since it doesn't do that much else. If you just want to print the key-value pairs seperated by the & symbol, and each joined by an =, that is straightforward using str.join and str.format:

print("&".join("{}={}".format(key, value) for key, value in payload.items()))

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.