python字符串-编码(encode)
encode方法用于使用指定的编码格式对字符串进行编码。
语法
encode(encoding='utf-8', errors='strict')
参数
- encoding: 编码格式,默认为‘utf-8’。
- errors: 不同错误的处理方案,默认值为strict。
- strict:遇到非法字符就抛出异常。
- ignore:忽略非法字符。
- replace:用“?”替换非法字符。
- xmlcharrefreplace:使用 xml 的字符引用。
返回值
- 编码后的字符串。
示例
str = '我爱我的爸妈'
print('默认为utf-8编码:', str.encode())
print('utf-8明示编码:', str.encode('UTF-8'))
print('GBK编码:', str.encode('GBK', 'strict'))
try:
print('BIG5编码:', str.encode('BIG5', 'strict'))
except UnicodeError as err:
print('出错了:', err)
print('BIG5编码,igonre参数忽略非法字符:', str.encode('BIG5', 'ignore'))
默认为utf-8编码: b'\xe6\x88\x91\xe7\x88\xb1\xe6\x88\x91\xe7\x9a\x84\xe7\x88\xb8\xe5\xa6\x88'
utf-8明示编码: b'\xe6\x88\x91\xe7\x88\xb1\xe6\x88\x91\xe7\x9a\x84\xe7\x88\xb8\xe5\xa6\x88'
GBK编码: b'\xce\xd2\xb0\xae\xce\xd2\xb5\xc4\xb0\xd6\xc2\xe8'
出错了: 'big5' codec can't encode character '\u7231' in position 1: illegal multibyte sequence
BIG5编码,igonre参数忽略非法字符: b'\xa7\xda\xa7\xda\xaa\xba\xaa\xa8'
help()
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 as any other name registered with
codecs.register_error that can handle UnicodeEncodeErrors.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界