Convert bytes to a string

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

    Since this question is actually asking about subprocess output, you have more direct approaches available. The most modern would be using subprocess.check_output and passing text=True (Python 3.7+) to automatically decode stdout using the system default coding:

    text = subprocess.check_output(["ls", "-l"], text=True)
    

    For Python 3.6, Popen accepts an encoding keyword:

    >>> from subprocess import Popen, PIPE
    >>> text = Popen(['ls', '-l'], stdout=PIPE, encoding='utf-8').communicate()[0]
    >>> type(text)
    str
    >>> print(text)
    total 0
    -rw-r--r-- 1 wim badger 0 May 31 12:45 some_file.txt
    

    The general answer to the question in the title, if you're not dealing with subprocess output, is to decode bytes to text:

    >>> b'abcde'.decode()
    'abcde'
    

    With no argument, sys.getdefaultencoding() will be used. If your data is not sys.getdefaultencoding(), then you must specify the encoding explicitly in the decode call:

    >>> b'caf\xe9'.decode('cp1250')
    'café'
    

提交回复
热议问题