摘要:
下文讲述Python中encode()的方法的功能简介说明,如下所示:
encode()方法功能:
使用指定编码格式对字符串进行编码,
缺省编码格式为“utf-8”
encode()方法语法
s.encode([encoding='utf-8'][,errors='strict'])
--------参数说明--------
s:待编码的字符串
encoding:可选参数
待使用的编码
默认编码为 'utf-8'
errors:可选参数,
设置不同错误的处理方案
缺省值为 'strict'
为一个UnicodeError
其它值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace'
及通过 codecs.register_error() 注册的其它值
--------返回值说明--------
返回一个bytes对象,其内容为编码后的字符串
例:
字符串之encode()方法的示例分享
#maomao365.com
#endcode()函数的示例分享
S = "猫猫教程";
S_ = S.encode();
S_utf8 = S.encode("UTF-8");
S_gbk = S.encode("GBK");
print(S_)
print(S_utf8)
print(S_gbk)
//输出
b'\xe7\x8c\xab\xe7\x8c\xab\xe6\x95\x99\xe7\xa8\x8b'
b'\xe7\x8c\xab\xe7\x8c\xab\xe6\x95\x99\xe7\xa8\x8b'
b'\xc3\xa8\xc3\xa8\xbd\xcc\xb3\xcc'
Python之encode方法的示例分享