question 107 String Algorithms · medium
Read a web query string
A search page's address ends with a query string such as q=blue+dune&page=2&sort=. Turn it into a dictionary from name to value. Pairs are separated by &; each pair is split at its first = only; + in a value means a space; a pair with no = gets the value ""; empty pairs (from &&) are skipped; if a name appears twice, the later value wins.
Function
parse_query(query)Returnsa dictionary from name to text value
Example
"q=blue+dune&page=2&sort=" → {'q': 'blue dune', 'page': '2', 'sort': ''}Examples
callparse_query('q=blue+dune&page=2&sort=')returns{'q': 'blue dune', 'page': '2', 'sort': ''}
callparse_query('genre=sci-fi')returns{'genre': 'sci-fi'}
callparse_query('')returns{}