gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:25:44 25 4
gpt4 key购买 nike

我需要创建一个循环:

  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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com