0

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

1
  • Welcome to StackOverflow! Have you attempted to write this code on your own? If so, what do you have so far? Commented Apr 15, 2013 at 20:45

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think urlencode moved to urllib.parse.urlencode in Python 3.
@DSM, Yep. Just updated the doc link to point into the v3 docs from the 2.2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.