-1
stringSearch=[]
line2=input("What starting line number do you need? ")
line1=input("What closing line number do you need? ")


string = input("What string do you like to search in this test case: ")
with open('texfile') as f:
    lines = f.readlines()
    for line in lines:
        if line[int(line1):int(line2)].__contains__(string):
            stringSearch.append(line)
            print ("String found")
            print (stringSearch)

The list just comes up empty when the program runs. What can I do to set the input lines for the range, because I think that's where I made the mistake.

Ron Nabuurs
  • 1,528
  • 11
  • 29

1 Answers1

0

Suggestions

  1. 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
  1. 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

  1. 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]
​
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • wow thank you so much , i just have a quick question is it possible for the variables starting_line and ending_line to not be a user input ? Like it will contain a line number that contains a conditional string – mmmmmmmmmmm Apr 07 '21 at 06:35
  • @mmmmmmmmmmm--sure. But, can you give me an example so I can clarify? – DarrylG Apr 07 '21 at 06:37
  • for example , the system will find a keyword string "starting" and "exiting" in the file and save the line number where the string is found and store it to starting_line and ending_line. Im quite confused on what function to use because we have many text files and there are multiple "starting" and "exiting" keywords, also the number of "starting" keywords are higher than "exiting" – mmmmmmmmmmm Apr 07 '21 at 06:47
  • @mmmmmmmmmmm--would help to have an example file. Can you show a file example? If file is too large to add to your question could you place to contents in an online repository such as github or pastebin, etc. – DarrylG Apr 07 '21 at 06:55
  • heres a sample of the contents of the text file https://pastebin.com/XBsYks0H the starting_line is supposed to be "testCaseEntering" while the ending_line is "Fail " – mmmmmmmmmmm Apr 07 '21 at 07:17
  • @mmmmmmmmmmm--expanded answer to handle your case. Does this help? – DarrylG Apr 07 '21 at 08:34