2015-10-15 109 views
0

我试图从Popen中解析出来。输出看起来像一张表:在Python中解析命令输出

SceenShotAttached 如何打印col c? 我可以很容易地在shell脚本中使用awk,但我现在只依赖python。 谢谢!

+1

你应该添加你试过的代码。 – alexanderlukanin13

+0

我只尝试过pOpen和沟通到目前为止..看起来像引号越来越复杂:rshell = self.rSc(ip,“sar -r 1 3 | awk'{if(NR!= 1){print}} '| awk'{print $ 4}“)。谢谢! – user2013271

回答

0

最简单的方法是在标题中找到位置,然后对每一行采取相同的位置。事情是这样的:

proc_iter = iter(proc.stdout.splitlines()) 
header = proc_iter.next().split() 
col_idx = header.index('c') 
for row in proc_iter: 
    print row.split()[col_idx] 

如果你想成为更强大,支持多列,csv.DictReader是较重的,但功能更丰富的标准库的选择。