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


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

modifying parts of a urlparse.SplitResult

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

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


Contents

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

#101536 — modifying parts of a urlparse.SplitResult

FromTim Chase <python.list@tim.thechases.com>
Date2016-01-12 05:52 -0600
Subjectmodifying parts of a urlparse.SplitResult
Message-ID<mailman.55.1452601815.13488.python-list@python.org>
I can successfully parse my URLs.  However, I'd like to modify a part
then reassemble them.  However, like tuples, the parts appear to be
unmodifiable

  >>> URL = 'http://foo/path1/path2/?fragment=foo'
  >>> import urlparse
  >>> u = urlparse.urlparse(URL)
  >>> u  
  ParseResult(scheme='http', netloc='foo', path='/path1/path2/',
  params='', query='fragment=foo', fragment='')
  >>> u.query  
  'fragment=foo'
  >>> u.query = 'blah=baz'  
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  AttributeError: can't set attribute

The best I've been able to come up with is

  >>> 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?

-tkc


(sorry if this is a dupe post, as I got an SMTP bounce/error from
mail.python.org stating "service currently unavailable")

[toc] | [standalone]


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


csiph-web