严格搜索两个不同的文件

Strict searching against two different files

关于以下代码我有两个问题:

import subprocess

macSource1 = (r"\Server\path\name\here\dhcp-dump.txt")
macSource2 = (r"\Server\path\name\here\dhcp-dump-ops.txt")

with open (r"specific-pcs.txt") as file:
    line = []
    for line in file:
        pcName = line.strip().upper()
        with open (macSource1) as source1, open (macSource2) as source2:
            items = []
            for items in source1:
                if pcName in items:
                    items_split = items.rstrip("\n").split('\t')
                    ip = items_split[0]
                    mac = items_split[4]
                    mac2 = ':'.join(s.encode('hex') for s in mac.decode('hex')).lower()  # Puts the :'s between the pairs.
                    print mac2
                    print pcName
                    print ip

首先,如您所见,脚本正在针对 macSource1 的内容搜索 "specific-pcs.txt" 的内容以获取各种详细信息。我如何让它同时针对 macSource1 和 2 进行搜索(因为详细信息可能在任一文件中)??

其次,我需要有一个更严格的匹配过程,因为目前一台名为 'itroom02' 的机器不仅会找到它自己的详细信息,还会提供另一台名为“2nd-itroom02”的机器的详细信息.我怎样才能得到它?

非常感谢您的提前帮助! 克里斯

也许你应该像这样重组它:

macSources = [ r"\Server\path\name\here\dhcp-dump.txt",
               r"\Server\path\name\here\dhcp-dump-ops.txt" ]

with open (r"specific-pcs.txt") as file:
    for line in file:
        # ....
        for target in macSources:
            with open (target) as source:
                for items in source:
                   # ....

没有必要做line = [] 在你 for line in ...:.

之前

就 "stricter matching" 而言,由于你没有给出文件格式的示例,我只能猜测 - 但你可能想在你之后尝试 if items_split[1] == pcName:已经完成拆分,而不是拆分前的 if pcName in items:(假设名称在第二列 - 如果没有则相应调整)。