[Tutor] urlparse question

Erik Price erikprice@mac.com
Mon, 26 Aug 2002 09:17:02 -0400


On Monday, August 26, 2002, at 08:42  AM, Jaco.Smuts@za.didata.com wrote:

> using urlparse I can get
>
> urlValue[0] = 'http'
> urlValue[1] = 'www.google.com'
> urlValue[2] = '/search'
> urlValue[3] = ''
> urlValue[4] = 
> 'hl=en&lr=&ie=UTF-8&oe=utf-8&q=south+africa+travel+cape+town'
> urlValue[5] = ''
>
> What I'm looking for is a way (using a library, not regex if possible) 
> to
> parse out of urlValue[4]
> hl='en'
> lr=''
> ie='UTF-8&oe=utf-8'
> q='south+africa+travel+cape+town'
>
> Any ideas ?

qsDict = {}
for pair in urlValue[4].split('&'):
   pair = pair.split('=')
   qsDict[pair[0]] = pair[1]

This doesn't use a library, but it doesn't use regexes either, so it 
shouldn't be too slow.  Still, there's probably a more elegant solution 
using lambdas or list comprehensions, but I'm still waiting to hear back 
on another thread so I can figure out how to use them!  :)


Erik

PS: are you sure you wanted the "ie" variable to be "UTF-8&oe=utf-8", or 
was that a typo?  Because my solution won't know that it should do 
that....






--
Erik Price

email: erikprice@mac.com
jabber: erikprice@jabber.org