简体   繁体   中英

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 items manually, but how can I do this automatically? I want to do something like this:

# This doesn't work:
for k,v in s.items():
    print '%s : %s'%(k,v)

You could use the internal _asdict 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 a point raised in the comments, despite the prefix _ , which usually indicates a method not part of a public interface, the method is a public one. It's given the prefix to avoid name conflicts, as the namedtuple docs explain [link] :

To prevent conflicts with field names, the method and attribute names start with an underscore.

And in Python 3, this is much easier due to an implementation change:

>>> 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 the SplitResult is really a fancy tuple so you can use standard assignment as well.

>>> scheme, netloc, _, _, _ = s # I only want the scheme and netloc

Enjoy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM