How to iterate over urlparse.urlsplit () result Python
Let's say I have this code:
>>> import urlparse >>> url = "http://google.com" >>> s = urlparse.urlsplit(url) >>> print s SplitResult(scheme='http', netloc='google.com', path='', query='', fragment='') >>> print 'scheme ',s.scheme scheme http >>> print 'netloc ',s.netloc netloc google.com As you can see, I can iterate over the elements manually, but how to do it automatically? I want to do something like this:
# This doesn't work: for k,v in s.items(): print '%s : %s'%(k,v) You can use the _asdict internal method:
>>> import urlparse >>> url = "http://google.com" >>> s = urlparse.urlsplit(url) >>> s SplitResult(scheme='http', netloc='google.com', path='', query='', fragment='') >>> s._asdict() OrderedDict([('scheme', 'http'), ('netloc', 'google.com'), ('path', ''), ('query', ''), ('fragment', '')]) >>> d = s._asdict() >>> for k,v in d.items(): ... print k, repr(v) ... scheme 'http' netloc 'google.com' path '' query '' fragment '' To clarify the point raised in the comments, despite the _ prefix, which usually indicates that a method is not part of the public interface, this method is publicly available. This gave a prefix to avoid name conflicts, as namedtuple docs explain [link] :
To prevent conflicts with field names, method and attribute names start with an underscore.
And in Python 3, this is much simpler due to a change in implementation:
>>> vars(urllib.parse.urlsplit("http://www.google.ca")) OrderedDict([('scheme', 'http'), ('netloc', 'www.google.ca'), ('path', ''), ('query', ''), ('fragment', '')]) >>> url = "http://google.com" >>> s = urlparse.urlsplit(url) >>> scheme, netloc, path, query, fragment = s >>> scheme 'http' >>> netloc 'google.com' >>> path '' >>> query '' >>> fragment '' As shown above, SplitResult really is a fancy tuple, so you can also use the standard assignment.
>>> scheme, netloc, _, _, _ = s # I only want the scheme and netloc Enjoy.