Is there a simple method I'm missing in urllib
or other library for this task? URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
Here's an example of an input and my expected output:
Mozilla/5.0 (Linux; U; Android 4.0; xx-xx; Galaxy Nexus Build/IFL10C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla%2F5.0+%28Linux%3B+U%3B+Android+4.0%3B+xx-xx%3B+Galaxy+Nexus+Build%2FIFL10C%29+AppleWebKit%2F534.30+%28KHTML%2C+like+Gecko%29+Version%2F4.0+Mobile+Safari%2F534.30
For Python 2.x, use
urllib.quote
example:
EDIT:
In your case, in order to replace space by plus signs, you may use
urllib.quote_plus
example:
For Python 3.x, use
quote
and for string with space use
quote_plus
Keep in mind that both urllib.quote and urllib.quote_plus throw an error if an input is a unicode string:
As answered here on SO, one has to use 'UTF-8' explicitly:
Also, if you have a dict of several values, the best way to do it will be
urllib.urlencode
.