url编码
from urllib.parse import quote, unqoute
string = '你好啊'
en_str = 'hello'
utf8_code = quote(string)
print(utf8_code)
en_code = quote(en_str)
print(en_code)
gbk_code = quote(string, encoding='gbk')
print(gbk_code)
prot_str = unquote(gbk_code, encoding='gbk')
print(prot_str)
Base64编码
import base64
string = '你好啊'
b64_code = base64.b64encode(string.encode())
print(b64_code)
port_str = base64.b64decode(b64_code).decode()
print(port_str)
字符串转换ascii
string = '你好啊'
ascii_str = list(map(ord, string))
print(ascii_str)
prot_str = ''.join(map(chr, ascii_str))
print(prot_str)
md5加密
import hashlib
string = '你好啊'
md5 = hashlib.md5()
md5.update(string.encode('utf-8'))
code = md5.hexdigest()
print(code)
Unicode转中文
name = "王大锤"
unicode_name = name.encode("unicode_escape")
utf8_name = name.encode("utf-8")
gbk_name = name.encode("gbk")
gbk2312_name = name.encode("gb2312")
print(unicode_name)
print(utf8_name)
print(gbk_name)
print(gbk2312_name)
print(unicode_name.decode())
print(unicode_name.decode("unicode_escape"))
print(utf8_name.decode())
print(gbk_name.decode("gbk"))
转载自Python编程:对字符串加密的5种方式