我用python编写了一个包含file.log结果的解析文件,它生成了我的内存使用量和cpu和时间的一些数据,我想要的是在PyQt4接口的图形中输出结果而不是终端显示 在解析它之前,这是我的日志文件:
Memory: svmem(total=4014465024, available=3451576320, percent=14.0, used=1240055808, free=2774409216, active=543842304, inactive=525877248, buffers=82866176, cached=594300928)
CPU: 35.70 %
Elapsed execution time: 0.642941951752 seconds
这是我的解析文件:
import re
class Parse(object):
with open('./perf.log', 'r') as logFile:
for line in logFile:
# Searching for CPU metric
cpu_search_result = re.search('CPU: ([0-9.]*) %', line)
if cpu_search_result != None:
cpu_metric = cpu_search_result.group(1)
# Searching for Execution time metric
execution_time_search_result = re.search('Elapsed execution time: ([0-9.]*) seconds', line)
if execution_time_search_result != None:
execution_time_metric = execution_time_search_result.group(1)
# Searching for memory metric
memory_search_result = re.search('\((.*?)\)', line)
if memory_search_result != None:
metrics_splited = memory_search_result.group(1).split(",")
for pair in metrics_splited:
splited_key_value = pair.split("=")
metric_name = splited_key_value[0].strip()
metric_value = splited_key_value[1].strip()
memory_key_value_metrics[metric_name] = metric_value
# --------- end - parsing the file ---------
# --------- start - printing the result ---------
# Print all memory metrics if they exist
if (len(memory_key_value_metrics) > 0):
print "memory metrics: "
print "total = " + memory_key_value_metrics["total"]
print "available = " + memory_key_value_metrics["available"]
print "percent = " + memory_key_value_metrics["percent"]
print "used = " + memory_key_value_metrics["used"]
print "free = " + memory_key_value_metrics["free"]
print "active = " + memory_key_value_metrics["active"]
print "inactive = " + memory_key_value_metrics["inactive"]
print "buffers = " + memory_key_value_metrics["buffers"]
print "cached = " + memory_key_value_metrics["cached"]
# Print CPU Metric
if cpu_metric != -1:
print "CPU = " + cpu_metric
# Print Execution time metric
if execution_time_metric != -1:
print "Execution time = " + execution_time_metric
# --------- end - printing the result ---------
我很难看到我出错的地方,但是我再次对Python有些新意。非常感谢任何帮助。