python-3.x - 字节串解码的两种形式
问题描述
python3中以下两种转换有什么区别吗?
>>> b'hello'.decode()
'hello'
>>> b'hello'.decode('utf-8')
'hello'
就在今天,我遇到了第一种方法,这是我以前从未见过的(默认为 utf-8,还是没有明确设置和推断编码?
解决方案
默认值确实是 'utf-8'
>>> help(b'hello'.decode)
Help on built-in function decode:
decode(encoding='utf-8', errors='strict') method of builtins.bytes instance
Decode the bytes using the codec registered for encoding.
encoding
The encoding with which to decode the bytes.
errors
The error handling scheme to use for the handling of decoding errors.
The default is 'strict' meaning that decoding errors raise a
UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
as well as any other name registered with codecs.register_error that
can handle UnicodeDecodeErrors.
推荐阅读
- wpf - 当应用程序失去焦点/替代 IsKeyboardFocusWithin 时,最后一个焦点项目的视觉指示
- c# - CSVHelper - 忽略一行中的空白单元格,除非它们被填充
- postman - Postman 使用响应正文中的值调用多个 api
- python-3.x - 当我运行此代码时,我无法获得支路编号,而是获得了“
“。但我应该得到4。这是什么原因。我该怎么办? - three.js - 三色片段着色器
- sql - 删除旧 SQL 中的一行
- ruby-on-rails - Traceback(最近一次调用最后一次):ruby:没有这样的文件或目录——脚本/生成(LoadError)
- bash - 取一个逗号分隔的字符串并将其添加到命令中
- r - 如何在闪亮的可编辑数据表中指定文件名并限制列编辑
- javascript - 错误:当前未启用对实验性语法“可选链”的支持,但已启用