我获得了以下代码的帮助,但需要知道如何在所有比赛中都做到这一点。
result = []
# 1. Search for the first failed entry:
with open('diskDetails.txt', 'r') as f:
lines = f.readlines()
for idx, line in enumerate(lines):
if ': Failed' in line:
result = lines[idx-3:idx+25] # save lines from 'ID' to 'Sub Vendor'
#break # only the first match
# 2. Filter result:
keywords = ('ID', 'State', 'Capacity', 'Product ID', 'Serial No.')
result = list(filter(lambda l: str(l).startswith(keywords), result))
# 3. Save result:
with open('dataFinal.txt', 'wt') as f:
f.writelines(result)
因此,如果我取消注释“中断”,则仅显示第一个(如所述) (输出)
ID : 0:1:2
State : Failed
Capacity : 558.38 GB (599550590976 bytes)
Product ID : ST3600057SS
Serial No. : 6SL7YFWF
如果我注释掉,它只会在第一个之后显示一个匹配项。 (输出)
ID : 0:1:5
State : Failed
Capacity : 558.38 GB (599550590976 bytes)
Product ID : ST3600057SS
Serial No. : 6SLAWWHQ
如何获取它以返回文件中的所有匹配项(以上两个输出)?
diskDetails.txt示例。该文件可能包含4-8个部分,如下所示。我没有包括每个部分的每一行(以ID开头),因为每个部分长28行。假设关键字在每个部分中。同样,每个部分都用空白行分隔(如果有帮助的话)。
ID : 0:1:0
Status : Ok
Name : Physical Disk 0:1:0
State : Online
Power Status : Spun Up
Bus Protocol : SAS
Media : HDD
Part of Cache Pool : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted : No
Revision : ES66
Driver Version : Not Applicable
Model Number : Not Applicable
T10 PI Capable : No
Certified : Yes
Encryption Capable : No
Encrypted : Not Applicable
Progress : Not Applicable
Mirror Set ID : 0
Capacity : 558.38 GB (599550590976 bytes)
Used RAID Disk Space : 558.38 GB (599550590976 bytes)
ID : 0:1:1
Status : Ok
Name : Physical Disk 0:1:0
State : Online
Power Status : Spun Up
Bus Protocol : SAS
Media : HDD
Part of Cache Pool : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted : No
Revision : ES66
Driver Version : Not Applicable
Model Number : Not Applicable
T10 PI Capable : No
Certified : Yes
Encryption Capable : No
Encrypted : Not Applicable
Progress : Not Applicable
Mirror Set ID : 0
Capacity : 558.38 GB (599550590976 bytes)
Used RAID Disk Space : 558.38 GB (599550590976 bytes)
ID : 0:1:2
Status : Critical
Name : Physical Disk 0:1:2
State : Failed
Power Status : Spun Up
Bus Protocol : SAS
Media : HDD
Part of Cache Pool : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted : No
Revision : ES66
Driver Version : Not Applicable
Model Number : Not Applicable
T10 PI Capable : No
Certified : Yes
Encryption Capable : No
Encrypted : Not Applicable
Progress : Not Applicable
Mirror Set ID : 0
Capacity : 558.38 GB (599550590976 bytes)
ID : 0:1:3
Status : Ok
Name : Physical Disk 0:1:3
State : Online
Power Status : Spun Up
Bus Protocol : SAS
Media : HDD
Part of Cache Pool : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted : No
Revision : ES66
Driver Version : Not Applicable
Model Number : Not Applicable
T10 PI Capable : No
Certified : Yes
Encryption Capable : No
Encrypted : Not Applicable
Progress : Not Applicable
Mirror Set ID : 0
Capacity : 558.38 GB (599550590976 bytes)
Used RAID Disk Space : 558.38 GB (599550590976 bytes)
ID : 0:1:4
Status : Ok
Name : Physical Disk 0:1:4
State : Online
Power Status : Spun Up
Bus Protocol : SAS
Media : HDD
Part of Cache Pool : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted : No
Revision : ES64
Driver Version : Not Applicable
Model Number : Not Applicable
T10 PI Capable : No
Certified : Yes
Encryption Capable : No
Encrypted : Not Applicable
Progress : Not Applicable
Mirror Set ID : 0
Capacity : 558.38 GB (599550590976 bytes)
Used RAID Disk Space : 558.38 GB (599550590976 bytes)
ID : 0:1:5
Status : Non-Critical
Name : Physical Disk 0:1:5
State : Failed
Power Status : Spun Up
Bus Protocol : SAS
Media : HDD
Part of Cache Pool : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted : Yes
Revision : ES66
Driver Version : Not Applicable
Model Number : Not Applicable
T10 PI Capable : No
Certified : Yes
Encryption Capable : No
Encrypted : Not Applicable
Progress : Not Applicable
Mirror Set ID : Not Applicable
Capacity : 558.38 GB (599550590976 bytes)
Used RAID Disk Space : 558.38 GB (599550590976 bytes)
ID : 0:1:6
Status : Ok
Name : Physical Disk 0:1:6
State : Online
Power Status : Spun Up
Bus Protocol : SAS
Media : HDD
Part of Cache Pool : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted : No
Revision : ES66
Driver Version : Not Applicable
Model Number : Not Applicable
T10 PI Capable : No
Certified : Yes
Encryption Capable : No
Encrypted : Not Applicable
Progress : Not Applicable
Mirror Set ID : 0
Capacity : 558.38 GB (599550590976 bytes)
Used RAID Disk Space : 558.38 GB (599550590976 bytes)
如何仅输出和保存包含“失败”并且仅包含包含每个部分的关键字的行的部分?
答案 0 :(得分:2)
更改1:将结果追加到列表中。在每个循环上重新分配之前
if ': Failed' in line:
result.append(lines[idx-3:idx+25])
更改2:更新过滤器逻辑以遍历结果,因为它现在是列表列表
results = list(filter(lambda l: str(l).startswith(keywords), r) for r in result)
更改3:遍历结果并写入文件
with open('dataFinal.txt', 'wt') as f:
for result in results:
f.writelines(result)
希望这会有所帮助!
答案 1 :(得分:1)
您用“结果= []”行创建了一个空列表,这很好。
您重新定义列表“结果”,每次通过for循环使用以下行: 结果=线[idx-3:idx + 25]
您要使用以下内容将for循环添加到列表“结果”: result.append(lines [idx-3:idx + 25])