Path: csiph.com!weretis.net!feeder4.news.weretis.net!storethat.news.telefonica.de!feedme.news.telefonica.de!telefonica.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: modifying parts of a urlparse.SplitResult Date: Tue, 12 Jan 2016 05:52:36 -0600 Lines: 34 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de SUxNUg9XmCO4GUcrS8Zd2gCiHkAWfLuax5E0LoC/aWhQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'modify': 0.04; '(sorry': 0.09; 'lst': 0.09; 'stating': 0.09; 'tuple': 0.09; 'index': 0.13; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'magic': 0.16; 'pythonic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'url:foo': 0.16; 'attribute': 0.18; '>>>': 0.20; '"",': 0.22; 'parse': 0.22; 'smtp': 0.23; 'import': 0.24; '(most': 0.24; "i've": 0.25; 'appear': 0.26; "skip:' 10": 0.28; 'skip:u 20': 0.28; 'skip:q 20': 0.29; 'received:184': 0.30; "i'd": 0.31; "can't": 0.32; 'traceback': 0.33; 'file': 0.34; 'this?': 0.34; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'received:10': 0.37; 'charset:us-ascii': 0.37; 'skip:p 20': 0.38; 'takes': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'skip:u 10': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'post,': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1452599693209:2487431374 X-MC-Ingress-Time: 1452599693209 X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101536 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 "", line 1, in 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")