从零开始的Linux运维屌丝之路,资源免费分享平台   运维人员首选:简单、易用、高效、安全、稳定、社区活跃的开源软件

19、python中的encode()和decode()函数

发布:蔺要红04-15分类: Python

 

encode() 方法以指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案
该方法返回编码后的字符串,它是一个 bytes 对象

[root@test01 ~]# cat 1.py 
#!/usr/bin/python3
str = "中国您好";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")
print(str)
print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk)
print("UTF-8 解码:", str_utf8.decode('UTF-8','strict'))
print("GBK 解码:", str_gbk.decode('GBK','strict'))
[root@test01 ~]# python 1.py 
中国您好
UTF-8 编码: b'\xe4\xb8\xad\xe5\x9b\xbd\xe6\x82\xa8\xe5\xa5\xbd'
GBK 编码: b'\xd6\xd0\xb9\xfa\xc4\xfa\xba\xc3'
UTF-8 解码: 中国您好
GBK 解码: 中国您好

decode() 方法以指定的编码格式解码 bytes 对象。默认编码为 'utf-8'
该方法返回解码后的字符串

[root@test01 ~]# cat 12.py
#!/usr/bin/python3
str = "中国您好";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")
print(str)
print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk)
print("UTF-8 解码:", str_utf8.decode('UTF-8','strict'))
print("GBK 解码:", str_gbk.decode('GBK','strict'))
[root@test01 ~]# python 12.py 
中国您好
UTF-8 编码: b'\xe4\xb8\xad\xe5\x9b\xbd\xe6\x82\xa8\xe5\xa5\xbd'
GBK 编码: b'\xd6\xd0\xb9\xfa\xc4\xfa\xba\xc3'
UTF-8 解码: 中国您好
GBK 解码: 中国您好
 

      decode                 encode
str ---------> str(Unicode) ---------> str

Python decode() 方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码
>>> u = '中文'                 # 指定字符串类型对象u 

>>> str1 = u.encode('gb2312')  # 以gb2312编码对u进行编码,获得bytes类型对象
>>> print(str1)
b'\xd6\xd0\xce\xc4'

>>> str2 = u.encode('gbk')     # 以gbk编码对u进行编码,获得bytes类型对象
>>> print(str2)
b'\xd6\xd0\xce\xc4'
>>> str3 = u.encode('utf-8')   # 以utf-8编码对u进行编码,获得bytes类型对象
>>> print(str3)
b'\xe4\xb8\xad\xe6\x96\x87'

>>> u1 = str1.decode('gb2312') # 以gb2312编码对字符串str进行解码,获得字符串类型对象
>>> print('u1')
'中文'

>>> u2 = str1.decode('utf-8')  # 报错,因为str1是gb2312编码的
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte
 
温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,如有侵权我会在24小时之内删除!

欢迎使用手机扫描访问本站