Python urlparse.unparse_qsl?

871 Views Asked by At

In Python's urlparse, you can use urlparse to parse the URL, and then parse_qsl to parse the query.

I want to remove a query (name, value) pair, and then reconstruct the URL.

There is a urlunparse method, but no unparse_qsl method.

What is the correct way to reconstruct the query from the qsl list?

2

There are 2 best solutions below

0
On
>>> urlparse.parse_qsl('q=stackoverflow')
[('q', 'stackoverflow')]

>>> urllib.urlencode(urlparse.parse_qsl('q=stackoverflow'))
'q=stackoverflow'
1
On

The function urllib.urlencode is appropriate.