import re
def getProcesses(usage=80.0, filename='performance.txt'):
with open(filename) as f:
lines = f.readlines()
for line in lines:
m = re.search('^\d+.*\s+(\d+\.\d+)\s+(\d+\.\d+).*\:', line)
if m:
cpu_usage = m.group(1)
if float(cpu_usage) > usage:
print "CPU: ", cpu_usage
print line
getProcesses()
将序号和处理的文本放到一个元组里面,然后将元组存储到列表中,再按元组中的序号排列。
试试?
import psutil
imoprt 大法好