读取shell命令执行结果,生成字典,并计算差值

getvalues.py

#!/usr/bin/env python

# -*-coding:UTF-8-*-
##Edit by vrocwang
##

import commands
import re

def Values(**v):
    #get_values = '/home/redis/redis-cli -p 6481 info | grep -E keyspace_hits|keyspace_misses'
    get_values = 'cat test'
    status,result = commands.getstatusoutput(get_values)
    Lists = re.split('=|\n',result)
    if status != 0:
        return 'Wrong'
    else:
        v = {
        Lists[0]:Lists[1],
        Lists[2]:Lists[3]
        }
        value = int(v[Lists[0]]) - int(v[Lists[2]])
        return value

if __name__ == "__main__":
    print Values()