2014-02-06 56 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']} 
相关问题