Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101541 > unrolled thread

Re: modifying parts of a urlparse.SplitResult

Started byTim Chase <python.list@tim.thechases.com>
First post2016-01-12 07:15 -0600
Last post2016-01-12 07:15 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: modifying parts of a urlparse.SplitResult Tim Chase <python.list@tim.thechases.com> - 2016-01-12 07:15 -0600

#101541 — Re: modifying parts of a urlparse.SplitResult

FromTim Chase <python.list@tim.thechases.com>
Date2016-01-12 07:15 -0600
SubjectRe: modifying parts of a urlparse.SplitResult
Message-ID<mailman.62.1452604696.13488.python-list@python.org>
On 2016-01-12 13:46, Peter Otten wrote:
> Tim Chase wrote:
> >   >>> u = urlparse.urlsplit(URL)
> >   >>> lst = list(u) # can't manipulate the tuple directly
> >   >>> lst[3] = "bar=baz" # 3 = query-string index
> >   >>> urlparse.urlunsplit(lst)
> >   'http://foo/path1/path2/?bar=baz'
> > 
> > It takes knowing that "3" is the magic index (documented, but not
> > given some named-constant in urlparse) for the query-string.  Is
> > there some better, clearer, or more Pythonic way to do this?
> 
> To allow more fieldnames namedtuples use method names starting with
> an underscore. You can safely use them, they are not private. So:
> 
> >>> pr = urlparse.urlparse("http://foo/path1/path2/?fragment=foo")
> >>> urlparse.urlunparse(pr._replace(query="bar=baz"))
> 'http://foo/path1/path2/?bar=baz'

Much nicer.  Thanks!

Off to improve my code.

-tkc


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web