第一次匹配后如何在文件行中停止匹配

时间:2018-01-18 14:25:32

标签: python python-3.x for-loop

我需要创建一个循环:

  1. 读取列表中文件的内容,其格式为Hostname-YYMMDD.txt;

  2. 匹配此文本文件中某一行的特定内容;

  3. 停在第一场比赛(忽略重复);

  4. 在Excel工作表中打印此行的特定部分。

  5. 到目前为止,我在第3点失败了。

    import os
    import xlsxwriter
    import re
    
    MyPath = "FileDirectory" #e.g. "MyDocuments/Python"
    MyHost = "Hostname" # e.g. "Router1_Loc1"
    Host_Probes = []
    
    # Loop: Populate Host_Probes []
    for root, dirs, files in os.walk(MyPath, topdown=False):
        for names in files:
            if MyHost in names:
                Host_Probes.append((os.path.join(names)))
    
    # List with locations of all log files for the TargetHost
    Probe_Paths = [MyPath + s for s in Host_Probes]
    
    # Excel file and sheet:
    workbook = xlsxwriter.Workbook('MyFile'.xlsx)
    worksheet = workbook.add_worksheet('Sheet1')
    row = 2 #Row:3
    col = 2 #Col:C
    
    # Here I "tell" Python to write the Line that says "CPU utilization" 
    # For a given day and then write the CPU utilization for the next day
    # in the next column:
    
    for s in Probe_Paths:
        with open (s) as Probe:
            for fileLine in Probe:
                if "Core0: CPU utilization" in fileLine:
                    worksheet.write(row, col, int(re.sub('[^0-9]', '', fileLine)))
                elif "Core1: CPU utilization" in fileLine:
                    worksheet.write(row +1, col, int(re.sub('[^0-9]', '', fileLine)))
                    col +=1
    Probe.close()
    
    worksheet
    workbook.close()
    

    麻烦的是这个输出是重复的一些文件,因此它不是填充一次,而是在文件中写入两次。

    我无法使内容的循环停止匹配" Core0:CPU利用率"和" Core1:CPU利用率"在第一次遇到它之后。 有没有办法让Python只写第一个匹配并移动到列表Probe_Paths的下一个字符串?

    我希望有人可以提供建议。

2 个答案:

答案 0 :(得分:2)

您可以创建一个标志变量,指示您是否已经看到了要写的行

for s in Probe_Paths:
    with open (s) as Probe:
        seen = [0, 0]
        if "Core0: CPU utilization" in fileLine and not seen[0]:
            worksheet.write(row, col, int(re.sub('[^0-9]', '', fileLine)))
            seen[0] = 1
        elif "Core1: CPU utilization" in fileLine and not seen[1]:
            worksheet.write(row +1, col, int(re.sub('[^0-9]', '', fileLine)))
            seen[1] = 1
        col +=1

        # have both, can stop looking in the file
        # will not increment col for skipped lines
        if all(seen):
            break

答案 1 :(得分:1)

另一种方法是使用一个字典,其中包含密钥,例如<script src="https://code.highcharts.com/highcharts.js"></script> <div id="container" style="min-width: 310px; height: 600px; margin: 0 auto"></div>和布尔值。比之前检查那个布尔值:

Core0: CPU utilization