Using
urllib.quote_plus(url, safe=':')
Since you do not want to encode the colon, you need to indicate that when urllib.quote() called:
>>> expected = 'https:%2F%2Fdl.dropbox.com%2Fu%2F94943007%2Ffile.kml' >>> url = 'https://dl.dropbox.com/u/94943007/file.kml' >>> urllib.quote(url, safe=':') == expected True
urllib.quote() accepts the safe keyword argument, which is / by default and indicates which characters are considered safe and therefore need not be encoded. In the first example, you used '' , as a result of which slashes were encoded. The unexpected output that you inserted below where the slashes were not encoded was probably from a previous attempt, when you did not set the safe keyword argument at all.
Overriding the default value '/' and eliminating the colon with ':' is what ultimately gives the desired result.
Change In addition, the API requires spaces to be encoded as plus signs. Therefore, you should use urllib.quote_plus() (whose key argument safe is not equal to '/' by default).
source share