Python – Convert spaces to %20 in list


I need to convert spaces to %20 for api posts in a python array

tree = et.parse(os.environ['SPRINT_XML'])
olp = tree.findall(".//string")
if not olp:
  print colored('FAILED', 'red') +" No jobs accociated to this view"
joblist = [t.text for t in olp]

How can I do that to t.text above?

Best Solution

I would recommend using urllib.parse module and its quote() function. Example for Python3:

from urllib.parse import quote
text_encoded = quote(t.text)

Note: using quote_plus() won't work in your case as this function replaces spaces by plus char.