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

标签 python python-3.x for-loop

我需要创建一个循环:

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

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

  3. 在第一次匹配时停止(忽略重复项);

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

到目前为止,我在第 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 的下一个字符串?

希望有人指点一下。

最佳答案

你可以创建一个标志变量来指示你是否已经看到你想写的行

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

关于python - 如何在第一次匹配后停止匹配文件行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48323523/

相关文章:

python - 如何在打字时用咒语给单词着色?

python - 如何将 JSON 数据转换为 HTML 表格?

Python Popen - 环境 - ffmpeg 崩溃

python - 类型错误 : 'module' object is not callable Python3

python - 'second_file' 没有属性 'hello' -- Discord Python Bot

python - 如何在 Python 多处理中的所有进程之间共享数据?

Python tkinter 销毁窗口

python - 如何根据字典中的用户名和日期搜索数据框

r - 具有最大操作的高效双 for 循环

增强for循环下的Java if语句不起作用