用python解析输出文件并创建两个列表

2024-09-21 10:48:22 发布

您现在位置: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

Tags: 文件version状态计算机错误drivetoolall
2条回答
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']}

如何跟踪当前计算机的状态,然后检查错误消息。你知道吗

未测试代码-

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)

相关问题 更多 >

    热门问题