Convert bytes to a string

后端 未结 19 2236
野性不改
野性不改 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 05:13

    While @Aaron Maenpaa's answer just works, a user recently asked:

    Is there any more simply way? 'fhand.read().decode("ASCII")' [...] It's so long!

    You can use:

    command_stdout.decode()
    

    decode() has a standard argument:

    codecs.decode(obj, encoding='utf-8', errors='strict')

提交回复
热议问题