Convert bytes to a string

后端 未结 19 2247
野性不改
野性不改 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

    You need to decode the byte string and turn it in to a character (Unicode) string.

    On Python 2

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

    or

    unicode('hello', encoding)
    

    On Python 3

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

    or

    str(b'hello', encoding)
    

提交回复
热议问题