2017-10-05 97 views
0

我想从字符串中获取特定的行列元素。如何从python中的字符串获取特定的行列元素?

import subprocess 
process = subprocess.Popen(['free','-m'],stdout=subprocess.PIPE) 
out, err = process.communicate() 
out = out.decode("utf-8") 
print(out) 

输出是:

   total  used  free  shared buff/cache available 
Mem:   3854  2778   299   351   776   407 
Swap:   3909   80  3829 

我想第三排,3ND列元素,也就是80 我怎样才能得到呢?

+1

分上线,然后分割线,挑3元 –

回答

1

解码一次,根据线分割,然后挑有趣线,采用str.split分裂并挑选相关领域。转换为整数

output = """    total  used  free  shared buff/cache available 
Mem:   3854  2778   299   351   776   407 
Swap:   3909   80  3829""" 

print(int(output.splitlines()[2].split()[2])) 

,让80预期

+0

谢谢,这帮助了我。 –

相关问题