Suggestions
- Variable naming
Why line2 followed by line1? Better to name in successive order i.e. line1, line2.
Or even better relate variable name to its purpose and use Python naming convention thus:
starting_line, ending_line, search_string, search_results
- Use in rather than __ contains__
Like all "Dunder" or "special methods" identified by names that begin and end with __, contains is not meant to be called directly but, in very specific cases Reference
lines contain your array of lines, so use slicing to get the desired lines i.e.
lines[starting_line:ending_line+1]
Answer to Original Question
starting_line = int(input("What starting line number do you need? "))
ending_line = int(input("What closing line number do you need? "))
search_string = input("What string do you like to search in this test case: ")
search_results = []
with open('texfile') as f:
lines = f.readlines()
for index, line in enumerate(lines[starting_line:ending_line+1], start = starting_line):
# Used slicing to filter to lines to search
if search_string in line:
line = line.rstrip() # remove the newline at end of line
search_results.append(line)
print (f'Found string in line number {index} containing {line}')
Answer to Revised Question
def get_groups(file, start_line, end_line):
'''
Finds multiple groups of data lines
file - file path
start_line - substring for starting line
end_line - substring for ending line
'''
with open(file) as f:
result = []
processing_group = False
for line in f:
line = line.rstrip()
if start_line in line:
# Start new group
result.append([])
result[-1].append(line) # First line of group
processing_group = True
elif end_line in line and processing_group:
result[-1].append(line) # last line of group
processing_group = False
elif processing_group:
result[-1].append(line) # Add this line to current group
return result
Usage
res = get_groups('test_cases.txt', 'src=testCaseEntering', 'src=testCaseEntering')
for group in res:
print('Test Case')
print(*group, sep = '\n')
File: 'test_cases.txt'
2021.02.28.19.12.31.121605] 1500790 [in=][src=main] sz_right <60>
[2021.02.28.19.12.31.124834] 1500793 [in=][src=main] sz_right </opt/svgCppTests/adi_wil_cfg_profile-2_24_100ms.cfg>
[2021.02.28.19.12.31.192254] 1500861 [in=][src=main] gDSessionConfig->nodeVersionMajorMF <0>
[2021.02.28.19.12.31.771470] 1501440 [in=][src=connect] Connected
[2021.02.28.19.12.32.892645] 1502561 [in=testAclEntrySetGet_INPSVG1297][src=testCaseEntering] testCaseEntered
[2021.02.28.19.12.34.114328] 1503783 [in=][src=getNetworkStatus]
5118136 [in=]error _rcCallback <1>
[2021.02.28.19.12.31.124834] 1500793 [in=][src=main] sz_right
[2021.02.28.20.12.50.643308] 5120312 [in=][src=setMode]
[2021.02.28.20.14.37.608403] 5227277 [in=testModesAll_INPSVG1367][src=testCaseExiting] ================================== Fail ===================================
[2021.02.28.19.12.32.892645] 1502559 [in=testAclEntrySetGet_INPSVG1297][src=testCaseEntering]
[2021.02.28.19.12.31.124834] 1500888 [in=][src=main] sz_right
[2021.02.28.19.12.31.124834] 1500888 [in=][src=main] sz_right
[2021.02.28.19.12.31.124834] 1500888 [in=][src=main] sz_right
[2021.02.28.20.14.37.608403] 5337289 [in=testModesAll_INPSVG1367][src=testCaseExiting]
[2021.02.28.19.12.32.892645] 1502559 [in=testAclEntrySetGet_INPSVG1297][src=testCaseEntering]
[2021.02.28.20.12.50.643308] 5120312 [in=][src=setMode]
[2021.02.28.20.14.37.608403] 5337289 [in=testModesAll_INPSVG1367][src=testCaseExiting] ================================== Fail ===================================
[2021.02.28.19.12.32.892645] 1502559 [in=testAclEntrySetGet_INPSVG1297][src=testCaseEntering]
[2021.02.28.20.14.38.401407] 5228070 [in=][src=connect] Connected
[2021.02.28.20.14.37.608403] 5337289 [in=testModesAll_INPSVG1367][src=testCaseExiting]
Output
Test Case
[2021.02.28.19.12.32.892645] 1502561 [in=testAclEntrySetGet_INPSVG1297][src=testCaseEntering] testCaseEntered
[2021.02.28.19.12.34.114328] 1503783 [in=][src=getNetworkStatus]
5118136 [in=]error _rcCallback <1>
[2021.02.28.19.12.31.124834] 1500793 [in=][src=main] sz_right
[2021.02.28.20.12.50.643308] 5120312 [in=][src=setMode]
[2021.02.28.20.14.37.608403] 5227277 [in=testModesAll_INPSVG1367][src=testCaseExiting] ================================== Fail ===================================
Test Case
[2021.02.28.19.12.32.892645] 1502559 [in=testAclEntrySetGet_INPSVG1297][src=testCaseEntering]
[2021.02.28.19.12.31.124834] 1500888 [in=][src=main] sz_right
[2021.02.28.19.12.31.124834] 1500888 [in=][src=main] sz_right
[2021.02.28.19.12.31.124834] 1500888 [in=][src=main] sz_right
[2021.02.28.20.14.37.608403] 5337289 [in=testModesAll_INPSVG1367][src=testCaseExiting]
Test Case
[2021.02.28.19.12.32.892645] 1502559 [in=testAclEntrySetGet_INPSVG1297][src=testCaseEntering]
[2021.02.28.20.12.50.643308] 5120312 [in=][src=setMode]
[2021.02.28.20.14.37.608403] 5337289 [in=testModesAll_INPSVG1367][src=testCaseExiting] ================================== Fail ===================================
Test Case
[2021.02.28.19.12.32.892645] 1502559 [in=testAclEntrySetGet_INPSVG1297][src=testCaseEntering]
[2021.02.28.20.14.38.401407] 5228070 [in=][src=connect] Connected
[2021.02.28.20.14.37.608403] 5337289 [in=testModesAll_INPSVG1367][src=testCaseExiting]