字节解码的两种形式

时间:2019-12-30 23:21:34

标签: python-3.x

在python3中以下两次转换之间是否有区别?

>>> b'hello'.decode()
'hello'
>>> b'hello'.decode('utf-8')
'hello'

今天我遇到了第一个方法,这是我以前从未见过的(默认为utf-8,还是没有明确设置和推断编码?

1 个答案:

答案 0 :(得分:2)

默认确实是'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.