每天进步一点点之python学习中decode()字符串方法记录。
功能:
python中decode()的方法也是一个字符串编码方面的方法,与之相对应的是encode()字符串方法,但是decode()负责的是将字符串以指定的解码格式解码字符串。
使用格式如下:
str.decode(encoding=’UTF-8′,errors=’strict’)
参数说明:
str:需要解码的字符串;
encoding:指定的解码编码。
errors:是用来设置不同错误的处理方案的,默认值是strict。
返回值:
python的decode()方法将会返回经过指定解码编码解码后的新字符串。
使用测试笔记:
>>> str=”我爱学习”
>>> str2=str.encode(“UTF-8”) #使用encode编码
>>> str2
b’\xe6\x88\x91\xe7\x88\xb1\xe5\xad\xa6\xe4\xb9\xa0′
>>> str2.decode(“UTF-8”) #使用decode解码
‘我爱学习’
PS:需要注意的是编码和解码的字符编码一定要一致,否则可能出现乱码。