Python子进程输出格式?

Python子进程输出格式?,python,linux,subprocess,Python,Linux,Subprocess,我想用python打印Pi的操作系统信息。 操作系统命令“cat/etc/OS-release”在具有良好行格式的终端中运行良好 在Python中,我使用了: import subprocess output = subprocess.check_output("cat /etc/os-release", shell=True) print("Version info: ",output) 这很有效,但我没有收到任何新行: Version info: b'PRETTY_NAME="Rasp

我想用python打印Pi的操作系统信息。 操作系统命令“cat/etc/OS-release”在具有良好行格式的终端中运行良好

在Python中,我使用了:

import subprocess

output = subprocess.check_output("cat /etc/os-release", shell=True)
print("Version info: ",output)
这很有效,但我没有收到任何新行:

Version info:  b'PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"\nNAME="Raspbian GNU/Linux"\nVERSION_ID="9"\nVERSION="9 (stretch)"\nID=raspbian\nID_LIKE=debian\nHOME_URL="http://www.raspbian.org/"\nSUPPORT_URL="http://www.raspbian.org/RaspbianForums"\nBUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"\n'

如何格式化输出以添加换行?

问题在于,您的字符串是一个bytestring,如输出中所示,以字母b作为字符串前缀: 版本信息:b'PRETTY_NAME=“Raspbian GNU/Linux

一个简单的修复方法是按如下方式解码字符串:

import subprocess

output = subprocess.check_output("cat /etc/os-release", shell=True)
output = output.decode("utf-8")
print("Version info: ",output)
并且结果将正确打印。如果在解码前后打印其类型,则可以验证这是不同的对象:

import subprocess

output = subprocess.check_output("cat /etc/os-release", shell=True)
print(type(output))
output = output.decode("utf-8")
print(type(output))
这将产生以下输出:

<class 'bytes'>
<class 'str'>

str(output)的可能重复将起作用!很好的干净解决方案!