我需要将空格转换为%20,用于python数组中的api帖子
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]
我怎么能这样做t.text上面?
使用String.replace()
此处描述的方法:http://www.tutorialspoint.com/python/string_replace.htm
所以t.text
,它会t.text.replace(" ", "%20")
使用urllib.quote_plus:
import urllib ... joblist = [urllib.quote_plus(t.text) for t in olp]
我建议使用urllib.parse
模块及其quote()
功能.
https://docs.python.org/3.6/library/urllib.parse.html#urllib.parse.quote
Python3示例:
import urllib text_encoded = urllib.parse.quote(t.text)
注意:使用quote_plus()
将不适用于您的情况,因为此函数用加号替换空格.