Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
  exit(1)
joblist = [t.text for t in olp]

How can I do that to t.text above?

like image 943
user2363318 Avatar asked Dec 18 '14 21:12

user2363318


3 Answers

Use the String.replace() method as described here: http://www.tutorialspoint.com/python/string_replace.htm

So for t.text, it would be t.text.replace(" ", "%20")

like image 69
mbomb007 Avatar answered Nov 18 '22 20:11

mbomb007


I would recommend using urllib.parse module and its quote() function. https://docs.python.org/3.6/library/urllib.parse.html#urllib.parse.quote 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.

like image 39
Jan Rozycki Avatar answered Nov 18 '22 21:11

Jan Rozycki


Use urllib.quote_plus for this:

import urllib

...

joblist = [urllib.quote_plus(t.text) for t in olp]
like image 7
zmbq Avatar answered Nov 18 '22 20:11

zmbq