python - Parse value from string -


i have:

url = 'http://example.com/json?key=12345&lat=52.370216&lon=4.895168&status=upcoming&radius=20&offset=0' 

how can parse value 20 parameter radius?

i think not possible urlparse.parse_qs(), isn't it? there better way rather using regex?

yes, use parse_qs():

parse query string given string argument (data of type application/x-www-form-urlencoded). data returned dictionary. dictionary keys unique query variable names , values lists of values each name.

>>> urlparse import parse_qs >>> url = 'http://example.com/json?key=12345&lat=52.370216&lon=4.895168&status=upcoming&radius=20&offset=0' >>> parse_qs(url)['radius'][0] '20' 

upd: @danielroseman noted (see comments), should first pass url through urlparse:

>>> urlparse import parse_qs, urlparse >>> parse_qs(urlparse(url).query)['radius'][0] '20' 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -