python: encode a url with percentage signs?
Question: I am trying to convert the following url

    
    
    http://www.website.com/search/si/1/doctors/Vancouver, BC
    

to

    
    
    http://www.website.com/search/si/1/doctors/Vancouver%2C%20BC
    

I tried

    
    
    urllib.quote('http://www.website.com/search/si/1/doctors/Vancouver, BC', '')
    

and it resulted in replacing everything with a percentage sign.

What's the proper way to do this?


Answer: Use
[`urllib.quote()`](https://docs.python.org/2/library/urllib.html#urllib.quote)
for the url `path` leaving everything else as is:

    
    
    from urllib import quote
    from urlparse import urlparse, urlunparse
    
    url = "http://www.website.com/search/si/1/doctors/Vancouver, BC"
    
    scheme, netloc, path, params, query, fragment = urlparse(url)
    path = quote(path)
    print urlunparse((scheme, netloc, path, params, query, fragment))
    

prints:

    
    
    http://www.website.com/search/si/1/doctors/Vancouver%2C%20BC
    

See also:

  * [How can I normalize a URL in python](http://stackoverflow.com/questions/120951/how-can-i-normalize-a-url-in-python)

