>>>help(str.encode)
Help on built-in function encode:
encode(encoding='utf-8', errors='strict') method of builtins.str instance
Encode the string using the codec registered for encoding.
encoding
The encoding in which to encode the string.
errors
The error handling scheme to use for encoding errors.
The default is'strict' meaning that encoding errors raise a
UnicodeEncodeError. Other possible values are 'ignore','replace'and'xmlcharrefreplace'as well asany other name registered with
codecs.register_error that can handle UnicodeEncodeErrors.
>>> str1='一'>>> str1.encode()b'\xe4\xb8\x80'>>> str1.encode('utf-16')b'\xff\xfe\x00N'>>> str1.encode('gb2312')b'\xd2\xbb'>>> str1.encode('gbk')b'\xd2\xbb'>>> str1.encode('ASCII')
Traceback (most recent call last):
File "<pyshell#86>", line 1,in<module>
str1.encode('ASCII')
UnicodeEncodeError:'ascii' codec can't encode character '\u4e00' in position 0: ordinal notinrange(128)>>> str1.encode('ASCII','ignore')#忽略不能编码的字符,结果中不包含不能编码的字符,且没有任何提示b''>>> str1.encode('ASCII','replace')#使用问号 ? 替换不能进行编码的字符b'?'