I am new to python.My text file has the below information
我是python的新手。我的文本文件有以下信息
15:50:12 RECID: C642 SORD=000000000 Image=000000001
15:50:12 STEP 2: BUILD ICC KEY MAINTENANCE
15:50:12 RECID: C642 Image=000000000 EORD=000000007
15:50:12 STEP 3: COUNT OF RECORDS UDPATED
02:26:12 CPSE0152E 02.26.13 IS-0001 SS-BSS SSU-BSS SE-008965 Image -UE027A0
02:26:12 010000A ABC-HS52
02:26:12 HS52 DEF-hs52
Line 5 (02:26:12) will contain “SE-“ and "Image"-XXXXXXX” where XXXXXXX=type of Image dump as coded. The next line will have “ABC-XXXX” where XXXX = segment name Line 3 should have “DEF-XXXX” . We only need these 3 lines.
第5行(02:26:12)将包含“SE-”和“Image”-XXXXXXX“,其中XXXXXXX =编码的图像转储类型。下一行将有“ABC-XXXX”,其中XXXX =段名称第3行应具有“DEF-XXXX”。我们只需要这3行。
"Image" keyword can occure so many places but I want to search the "Image" name along with the next line have information “ABC-XXXX” and “DEF-XXXX” and print the next 2 lines of the text
“图像”关键字可以出现这么多地方,但我想搜索“图像”名称以及下一行有信息“ABC-XXXX”和“DEF-XXXX”并打印下两行文字
my output should be
我的输出应该是
02:26:12 CPSE0152E 02.26.13 IS-0001 SS-BSS SSU-BSS SE-008965 Image-UE027A0
02:26:12 010000A ABC-HS52
02:26:12 HS52 DEF-hs52
1 个解决方案
#1
1
Here's a working example that will get you what you need. Assuming your input file is called "input".
这是一个可以满足您需求的工作示例。假设您的输入文件被称为“输入”。
with open("input", "r") as file:
output = ""
for line in file.readlines():
if "Image" in line:
output += line
elif "ABC" in line and "Image" in output:
output += line
elif "DEF" in line and "ABC" in output:
output += line
else:
output = ""
print(output)
#1
1
Here's a working example that will get you what you need. Assuming your input file is called "input".
这是一个可以满足您需求的工作示例。假设您的输入文件被称为“输入”。
with open("input", "r") as file:
output = ""
for line in file.readlines():
if "Image" in line:
output += line
elif "ABC" in line and "Image" in output:
output += line
elif "DEF" in line and "ABC" in output:
output += line
else:
output = ""
print(output)