Is there a cleaner way to modify some parts of a URL in Python 2?
For example
http://foo/bar -> http://foo/yah
At present, I'm doing this:
import urlparse
url = 'http://foo/bar'
# Modify path component of URL from 'bar' to 'yah'
# Use nasty convert-to-list hack due to urlparse.ParseResult being immutable
parts = list(urlparse.urlparse(url))
parts[2] = 'yah'
url = urlparse.urlunparse(parts)
Is there a cleaner solution?
Unfortunately, the documentation is out of date; the results produced by
urlparse.urlparse()
(andurlparse.urlsplit()
) use acollections.namedtuple()
-produced class as a base.Don't turn this namedtuple into a list, but make use of the utility method provided for just this task:
The
namedtuple._replace()
method lets you create a new copy with specific elements replaced. TheParseResult.geturl()
method then re-joins the parts into a url for you.Demo:
mgilson filed a bug report (with patch) to address the documentation issue.
I guess the proper way to do it is this way.
As using
_replace
private methods or variables is not suggested.