Is there a better way to write this URL Manipulation in Python?

Posted by dnolen on Stack Overflow See other posts from Stack Overflow or by dnolen
Published on 2010-05-20T12:01:33Z Indexed on 2010/05/20 12:10 UTC
Read the original article Hit count: 185

Filed under:

I'm curious if there's a simpler way to remove a particular parameter from a url. What I came up with is the following. This seems a bit verbose. Libraries to use or a more pythonic version appreciated.

parsed = urlparse(url)
if parsed.query != "":
    params = dict([s.split("=") for s in parsed.query.split("&")])
    if params.get("page"):
        del params["page"]
    url = urlunparse((parsed.scheme,
                      None,
                      parsed.path,
                      None,
                      urlencode(params.items()),
                      parsed.fragment,))
    parsed = urlparse(url)

© Stack Overflow or respective owner

Related posts about python