如何在Python3中编码/转义unicode文本?(How to encode/escape unicode text in Python3? [duplicate])

编程入门 行业动态 更新时间:2024-10-17 21:26:52
如何在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 answers

How 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.

更多推荐

本文发布于:2023-07-14 18:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   如何在   unicode   duplicate   escape

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!