Python 编码转换
URL编码
import urllib.parse as uparse
source = "中国"
print(uparse.quote(source))
二进制与字符串转换
# 二进制转字符
b = b"example"
print(b.decode())
print(b.decode("utf8"))
print(b.decode("GBK"))
# 字符转二进制
s = "example"
print(str.encode(s))
16进制转汉字
s = """
\xe5\xaf\xb9\xe8\xb1\xa1\xe5\x90\x8d\xe6\x97\xa0\xe6\x95\x88\xe3\x80\x82
"""
ss = s.encode('raw_unicode_escape')
print(ss)
sss = ss.decode()
print(sss)
输出:
b"\n\xe5\xaf\xb9\xe8\xb1\xa1\xe5\x90\x8d\xe6\x97\xa0\xe6\x95\x88\xe3\x80\x82\n
对象名无效。