如何在Python3中编码/转义unicode文本?(How to encode/escape unicode text in Python3? [duplicate])
这个问题在这里已有答案:
如何在Python中对一个查询字符串进行urlencode? 11个答案如何从python3字符串对象中创建unicode实体?
有smt _ 1 # ,我想生成smt%20_%201%20%23
This question already has an answer here:
How to urlencode a querystring in Python? 13 answersHow can I create unicode entities out of python3 string objects?
Having smt _ 1 #, I want to generate smt%20_%201%20%23
最满意答案
使用来自urllib.parse quote :
>>> from urrlib.parse import quote >>> quote('smt _ 1 #') 'smt%20_%201%20%23'由于您没有处理网址,因此您似乎不需要指定任何安全字符。 如果您还需要/要转义,请将safe=''传递给quote 。
Use quote from urllib.parse:
>>> from urrlib.parse import quote >>> quote('smt _ 1 #') 'smt%20_%201%20%23'Since you aren't dealing with a url, you don't seem to need to specify any safe characters. If you'd also require / to be escaped, pass safe='' to quote.
更多推荐
发布评论