Python: Simple query string manipulation

0 comments

With all the helpful libraries out there, you'd think Python would have an easier way of doing this!

Using urlparse() to get the ParseResult object only allows read-only access to the attributes, making it useless if you want to set the query field.

This solution comes from Ned Batchelder from StackOverflow. I found this to be much more useful than the stuff in urlparse and urllib libraries.

Create a helper class:

import urllib, urlparse

class Url(object):
def __init__(self, url):
"""
Construct from a string.
"""

self.scheme, self.netloc, self.path, self.params, self.query_string, self.fragment = urlparse.urlparse(url)
self.query = dict(urlparse.parse_qsl(self.query_string))

def build(self):
"""
Turn back into a URL.
"""
return u"%s" % urlparse.urlunparse((self.scheme, self.netloc, self.path, self.params, urllib.urlencode(self.query), self.fragment))

def __str__(self):
return self.build()

def __unicode__(self):
return self.build()

Then use like this:

u = Url(url)
del u.query['page']
print u

Slightly modified to use __unicode__() rather than __str__().

*update 23/08/2011*

  • Removed the use of urllib.urlencode() because it's deprecated. Now uses urllib.
  • Added __str__() to accompany __unicode__(). Added build() method.
  • Renamed args to be query.

Sources

0 comments:

Post a Comment

Leave your thoughts ...
---
If you are having trouble with copy/pasting in comments, you need to sign in or click 'Preview'. For more information about this Firefox bug, see here.

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog