I am trying to urlencode this string before I submit.
queryString = \'eventName=\' + evt.fields[\"eventName\"] + \'&\' + \'eventDescription=\' + evt.fie
for future references (ex: for python3)
>>> import urllib.request as req
>>> query = 'eventName=theEvent&eventDescription=testDesc'
>>> req.pathname2url(query)
>>> 'eventName%3DtheEvent%26eventDescription%3DtestDesc'
For use in scripts/programs which need to support both python 2 and 3, the six module provides quote and urlencode functions:
>>> from six.moves.urllib.parse import urlencode, quote
>>> data = {'some': 'query', 'for': 'encoding'}
>>> urlencode(data)
'some=query&for=encoding'
>>> url = '/some/url/with spaces and %;!<>&'
>>> quote(url)
'/some/url/with%20spaces%20and%20%25%3B%21%3C%3E%26'
The syntax is as follows :
import urllib3
urllib3.request.urlencode({"user" : "john" })
Try this:
urllib.pathname2url(stringToURLEncode)
urlencode
won't work because it only works on dictionaries. quote_plus
didn't produce the correct output.
In Python 3, this worked with me
import urllib
urllib.parse.quote(query)
What you're looking for is urllib.quote_plus:
>>> urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$')
'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'
In Python 3, the urllib
package has been broken into smaller components. You'll use urllib.parse.quote_plus (note the parse
child module)
import urllib.parse
urllib.parse.quote_plus(...)