2017-04-17 4 views
1

일치하는 정규식을 목록에로드하고 중간 값, 모드 및 평균을 계산하려고합니다.파이썬에서 평균, 모드, 평균

데이터 파일 (pc1.txt) :

2017-04-16 13:32:59 
\\desktop-XXXXXXX\processor(_total)\% processor time : 1.05614841124945 
\\desktop-XXXXXXX\memory\% committed bytes in use : 5.50960924380334 

2017-04-16 13:33:05 
\\desktop-XXXXXXX\processor(_total)\% processor time : 1.08875159384721 
\\desktop-XXXXXXX\memory\% committed bytes in use : 5.5102938969471 

2017-04-16 13:33:10 
\\desktop-XXXXXXX\processor(_total)\% processor time : 0 
\\desktop-XXXXXXX\memory\% committed bytes in use : 5.46869437193207 

BootTime 200938 

------------------------------------ 
------------------------------------ 

2017-04-16 13:40:11 
\\desktop-XXXXXXX\processor(_total)\% processor time : 4.37510327488846 
\\desktop-XXXXXXX\memory\% committed bytes in use : 4.438387242009 

2017-04-16 13:40:17 
\\desktop-XXXXXXX\processor(_total)\% processor time : 1.90625777477218 
\\desktop-XXXXXXX\memory\% committed bytes in use : 4.44426156598249 

2017-04-16 13:40:22 
\\desktop-XXXXXXX\processor(_total)\% processor time : 0.078229917076289 
\\desktop-XXXXXXX\memory\% committed bytes in use : 4.44589104046464 

BootTime 69920 

정규식 찾을 값 :

with open('pc1.txt') as f: 
    for line in f: 
     re.findall(processor, f) 

그러나, 난 :

Processor: ^[\\].+processor.+[: ](\d*\.?\d*) 
Memory: ^[\\].+memory.+[: ](\d*\.?\d*) 
Boottime: ^BootTime.(\d+) 

지금까지 내가하려 1) 값을 일치시킬 수 없습니다; 2) 목록에 넣는다; 3) 중앙값, 모드 및 평균을 계산합니다.

from statistics import mode 
mode([value1, value2]) 

하지만 여전히 나는 모두 함께 조각을 넣어 수 없습니다

나는 모드를 계산하는 방법을 기본 knowlege 있습니다. 또한, 나는 통계를 쉽고/쉽게 처리 할 수있는 다른 프로그래밍 언어에 대해서 열려 있습니다. 즉, 텍스트 파일에서

+0

정확히'processor_regex'은 무엇인가 ? – Vallentin

+0

파이썬 버전을 사용하고 있습니까? –

+0

Python2.7. 우분투 16.04. –

답변

0

일치하는 우리가 medianmean을 얻기 위해 변환 할 필요가 없습니다 floatsstrings이다, 나는 statistics 대신 numpy을 사용 :

import numpy as np 
import re 

with open('pc1.txt', 'r') as myfile: 
    data = myfile.read() 
    processor = re.findall(r"processor time : ([\d.]+)", data, re.IGNORECASE | re.DOTALL | re.MULTILINE) 
    processor = [float(i) for i in processor] # we convert the matching list of strings to floats 
    if processor: 
     print (np.median(processor)) 
     print (np.mean(processor)) 

    memory = re.findall(r"memory\\%.*?: ([\d.]+)", data, re.IGNORECASE | re.DOTALL | re.MULTILINE) 
    memory = [float(i) for i in memory] # we convert the matching list of strings to floats 
    if memory: 
     print (np.median(memory)) 
     print (np.mean(memory)) 

    boot_time = re.findall(r"BootTime ([\d]+)", data, re.IGNORECASE | re.DOTALL | re.MULTILINE) 
    boot_time = [float(i) for i in boot_time] # we convert the matching list of strings to floats 
    if boot_time: 
     print (np.median(boot_time)) 
     print (np.mean(boot_time)) 
+0

어떤 이유로 코드에서 아무 것도 출력하지 않습니다. 심지어 그것은 올바르게 보인다. –

+0

출력이 없습니다. –

+0

나는 정직하게 코드를 실행할 때 무엇이 ​​잘못 될 수 있는지 모른다. 내 측면에서 예상대로 작동한다. –