Need print out lines before and after the match - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need print out lines before and after the match (/thread-28918.html) |
Need print out lines before and after the match - tester_V - Aug-10-2020 Greeting! I’m trying to print 4 lines before and after a “match” found in a line. I do not understand how to do it with my current knowledge of Python but I found 2 snippets, One finds lines Before the match and one After the match. Need help putting them together. Thank you. from itertools import islice with open(myfile, "r") as f: lines = f.readlines() for index, line in enumerate(lines): if "FIND" in line: # print(line.rstrip()) print("".join(lines[max(0,index-4):index])) # print 4 lines preceeding it with open(myfile, "r") as f: for line in f: #print (line) if "FIND" in line: #print (line) #print("".join(line)) print ("".join(islice(f,4))) ### 4 Lines after match ### RE: Need print out lines before and after the match - deanhystad - Aug-10-2020 Why don't you just join lines[index-4] to lines[index+4]? I do not understand why you reopen the file. RE: Need print out lines before and after the match - tester_V - Aug-10-2020 I do not know what that means "Why don't you just join lines[index-4] to lines[index+4]?" Line ("".join(lines[max(0,index-4):index])) works fine. It is 4 lines after a "match' Im having problem with. I tried adding one more print statement but it does not print anything print("".join(lines[max(0,index-4):index])) # print 4 lines preceeding it print("".join(lines[max(0,index+4):index])) # print 4 lines after itthank you. RE: Need print out lines before and after the match - tester_V - Aug-10-2020 Any ideas how to do this? Thank you. RE: Need print out lines before and after the match - deanhystad - Aug-10-2020 print("".join(lines[max(0,index-4):min(index+4, len(lines)-1)])) RE: Need print out lines before and after the match - tester_V - Aug-11-2020 I underatand it is simple for you deanhystad but it looks amazing to me! Thank you for your help! Do you think you could elaborate what this part means and why I have it in the code "len(lines)-1"? Again, thanks man! RE: Need print out lines before and after the match - deanhystad - Aug-11-2020 It looks simple because it is simple. You are doing this: Quote:Find the letter d and return d, the letter before and the letter after.That is simple. When you are programming, how do you start? Do you write a detailed description of the problem you are trying to solve? I do. Even for simple programs like this. Writing down the problem description places it clearly in my mind. If I don't write down the problem description I may end up solving the wrong problem. The next thing I do is draw pictures of the problem and my solution. Everyone is a visual thinker and everyone will benefit from drawing. I always write the program on paper first. I fill notebooks with problem descriptions and notes and drawings and pseudocode. If you had drawn a picture of your solution with a short list and maybe some index values I bet you wouldn't have had any trouble writing the code. You are trying to do you designing with Python. You are not comfortable with Python and that makes it a terrible tool for doing design. My simple drawing above makes me realize there is a bug in my solution. Looking at it now I see the end of the slice was off. If you want the four before and the four after the slice should be lines[max(0,index-4):min(index+5, len(lines)) ] If index was 4 we would want lines 0 through 8. To get those lines the slice should be 0 to 9 where line[9] is not part of the slice.max(index+5, len(lines)) does the same thing you were doing with max(0,index-4) except for the end. Thinking some more about it I don't think the min is required. If you ask for a slice that extends beyond the source, the slice is "sliced" to fit.x = [0, 1, 2] print(x[0:10]) The same is not true if you specify a negative value for the start of slice.print(x[-2, 3]) The same as x[3-2, 3]
|