[英]Convert a string with escape sequences to their original character representation in python
[英]Python - Convert the escape character of encoded string to %
我尝试编码一个中文字符串
这是代码:
chinese = "公主连结"
print(chinese.encode()) -> b'\xe5\x85\xac\xe4\xb8\xbb\xe8\xbf\x9e\xe7\xbb\x93'
它返回的值是一个编码的 object, '\x'是一个转义字符,我如何像字符串一样使用这个 object
这是我的预期:
input: b'\xe5\x85\xac\xe4\xb8\xbb\xe8\xbf\x9e\xe7\xbb\x93'
output: %E5%85%AC%E4%B8%BB%E8%BF%9E%E7%BB%93
有人可以帮忙吗?
这看起来像 URL 编码。 你可以通过urllib.parse.quote
得到想要的结果:
chinese = "公主连结"
import urllib.parse
print(urllib.parse.quote(chinese))
结果:
%E5%85%AC%E4%B8%BB%E8%BF%9E%E7%BB%93
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.