2014-02-06 97 views
0

我有一些編程經驗,但我是python的新手。我有一個輸出文件,我正在分析。有兩種狀態,一臺計算機可能位於:解析輸出文件並用python創建兩個列表

  1. 「錯誤:未檢測到兼容的可信平臺模塊(TPM)。」 「
  2. 」錯誤:TPM已打開。「

我正在尋找一個簡單的程序,它需要輸出文件並創建兩個列表......一個是狀態爲#1的計算機,另一個是狀態爲#2的計算機。

輸出文件看起來是這樣的:

BitLocker Drive Encryption: Configuration Tool version 6.1.7601 
Copyright (C) Microsoft Corporation. All rights reserved. 

Computer Name: AAAAA 

ERROR: A compatible Trusted Platform Module (TPM) was not detected. 
--- 
BitLocker Drive Encryption: Configuration Tool version 6.1.7601 
Copyright (C) Microsoft Corporation. All rights reserved. 

Computer Name: BBBBBB 
+1

做三個連字符表明結束錯誤消息? – 2rs2ts

回答

1

怎麼樣的東西,跟蹤當前計算機的狀態,然後檢查錯誤消息。

未經測試的代碼 -

list1 = [] 
list2 = [] 
with open(file_name, 'r') as reader: 
    current_computer = None 
    for line in reader: 
     if line.startswith('Computer Name: '): 
      current_computer = line[len('Computer Name: '):] 
     if line.startswith('ERROR: A compatible Trusted Platform Module (TPM) was not detected.'): 
      list1.append(current_computer) 
     elif line.startswith('ERROR: The TPM is already on.'): 
      list2.append(current_computer) 
0
from collections import defaultdict 
import re 

def get_computer(row, reg=re.compile('Computer Name: (\S+)')): 
    m = reg.match(row) 
    return m and m.group(1) 

def line_starts_with(s): 
    return lambda line: line.startswith(s) 
found  = line_starts_with('ERROR: The TPM is already on.') 
not_found = line_starts_with('ERROR: A compatible Trusted Platform Module (TPM) was not detected.') 

def process_states(inf, value_fn, state_fns): 
    states_matched = defaultdict(list) 
    value = None 
    for row in inf: 
     nv = value_fn(row) 
     if nv: 
      value = nv 
     else: 
      for state_label,state_fn in state_fns.items(): 
       if state_fn(row): 
        states_matched[state_label].append(value) 
        break 
    return states_matched 

def main(): 
    with open('input.log') as inf: 
     results = process_states(inf, get_computer, {'found':found, 'not_found':not_found}) 

if __name__=="__main__": 
    main() 

然後進行測試,我皮棉下列內容:

import StringIO 

inf = StringIO.StringIO(""" 
BitLocker Drive Encryption: Configuration Tool version 6.1.7601 
Copyright (C) Microsoft Corporation. All rights reserved. 

Computer Name: AAAAA 

ERROR: A compatible Trusted Platform Module (TPM) was not detected. 
--- 
BitLocker Drive Encryption: Configuration Tool version 6.1.7601 
Copyright (C) Microsoft Corporation. All rights reserved. 

Computer Name: BBBBBB 

ERROR: The TPM is already on. 
--- 
BitLocker Drive Encryption: Configuration Tool version 6.1.7601 
Copyright (C) Microsoft Corporation. All rights reserved. 

Computer Name: CCCCCC 

ERROR: The TPM is already on. 
--- 
BitLocker Drive Encryption: Configuration Tool version 6.1.7601 
Copyright (C) Microsoft Corporation. All rights reserved. 

Computer Name: DDDDDDD 

ERROR: A compatible Trusted Platform Module (TPM) was not detected. 
""") 

results = process_states(inf, get_computer, {'found':found, 'not_found':not_found}) 

返回

{'found': ['BBBBBB', 'CCCCCC'], 'not_found': ['AAAAA', 'DDDDDDD']} 
相關問題