Reputation: 2063
I have a list of cities which have been stripped of punctuation and i need to format the URL correctly. My lists are ['New', 'York', 'NY'] and ['Lansing', 'MI'] and i need to format the query so that parameter assignments are seperated by the (&) symbol and words in the city are separated by the (+) sign.
For example it should look something like www.url.com/origin=New+York+NY&destination=Lansing+MI
Upvotes: 0
Views: 132
Reputation: 120486
From the urllib docs:
Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string
So
urllib.parse.urlencode({
'origin' : ' '.join(['New', 'York', 'NY']),
'destination' : ' '.join(['Lansing', 'MI'])
})
yields
'origin=New+York+NY&destination=Lansing+MI'
That documentation references the obsolete RFC 2396, but the differences between RFC 3986 and 2396 do not affect query string composition.
Upvotes: 2