Convert bytes to a string

后端 未结 19 2310
野性不改
野性不改 2020-11-21 04:45

I\'m using this code to get standard output from an external program:

>>> from subprocess import *
>>> command_stdout = Popen([\'ls\', \'-l         


        
19条回答
  •  死守一世寂寞
    2020-11-21 04:55

    In Python 3, the default encoding is "utf-8", so you can directly use:

    b'hello'.decode()
    

    which is equivalent to

    b'hello'.decode(encoding="utf-8")
    

    On the other hand, in Python 2, encoding defaults to the default string encoding. Thus, you should use:

    b'hello'.decode(encoding)
    

    where encoding is the encoding you want.

    Note: support for keyword arguments was added in Python 2.7.

提交回复
热议问题