如何获取python对象的内容<;类别'_io.TextIOWrapper'&燃气轮机;在使用子流程的管道时是否作为字符串?
我有个问题。我的子流程模块吐出了一些我不知道如何处理的东西 在ArchLinux上使用Python3 命令包括:如何获取python对象的内容<;类别'_io.TextIOWrapper'&燃气轮机;在使用子流程的管道时是否作为字符串?,python,python-3.x,encoding,subprocess,Python,Python 3.x,Encoding,Subprocess,我有个问题。我的子流程模块吐出了一些我不知道如何处理的东西 在ArchLinux上使用Python3 命令包括: svn信息 grep-IEi sed-e 尽管我的终端输出版本号为3,但我似乎无法将该值存储到Python变量中。这与我得到的是一个对象,而不是我怀疑的对象的内容有关。任何帮助都将不胜感激 为了完整起见,我演示了参数universal\u line如何影响输出 鉴于我的系统上的svn版本为3: 当shell=True,cwd=branch,universal\u lines=Fa
svn信息
grep-IEi
sed-e
3
,但我似乎无法将该值存储到Python变量中。这与我得到的是一个对象,而不是我怀疑的对象的内容有关。任何帮助都将不胜感激
为了完整起见,我演示了参数universal\u line
如何影响输出
鉴于我的系统上的svn版本为3
:
当shell=True,cwd=branch,universal\u lines=False时
结果:
结果:
此处未使用
Popen.communicate()
的返回值:
current_revision = process.stdout
您正在引用用于与shell通信的子流程
模块中的文件对象(现在已关闭)。您将完全忽略上一行返回的值
只需使用:
process_stdout = process.communicate()[0]
current_revision = process_stdout.decode('utf8').strip()
不使用通用\u换行符=True
时,或
process_stdout = process.communicate()[0]
current_revision = process_stdout.strip()
当你这样做的时候。数据包含一个换行符,因此调用str.strip()
如果只需要命令的stdout输出,则可以使用:
output = subprocess.check_output(
command, shell=True, cwd=branch, universal_newlines=True)
最后但并非最不重要的一点是,让Python解析响应可能更简单:
command = ('svn', 'info', branch)
output = subprocess.check_output(command, universal_newlines=True)
revision = None
for line in output.splitlines():
if line.startswith('Revision:'):
revision = line.partition(':')[1].strip()
break
ValueError: read of closed file
current_revision = process.stdout
process_stdout = process.communicate()[0]
current_revision = process_stdout.decode('utf8').strip()
process_stdout = process.communicate()[0]
current_revision = process_stdout.strip()
output = subprocess.check_output(
command, shell=True, cwd=branch, universal_newlines=True)
command = ('svn', 'info', branch)
output = subprocess.check_output(command, universal_newlines=True)
revision = None
for line in output.splitlines():
if line.startswith('Revision:'):
revision = line.partition(':')[1].strip()
break