0

I am working through a large document and need to find 2 specific, identical blocks of lines in the doc and perform different edits on each block. Of note, I need to be able to find the blocks off a keyword, and then edit the line which contains the keyword as well as the previous line.

I have tried to put together code from previous questions such as: python how to write to specific line in existing txt file

An example document would be:

   hundreds of lines of text
   more lines

   previous line1
   search_term line1

   previous line2
   search_term line 2

   more blocks of text

Here I want to find the two lines containing the search term, then edit them as well as their preceding lines.

below is a simplified example of the code I am trying with.

with open(end_file1, "r+") as f2:
    with open(act_end1, "w+") as f3:
        lines = f2.readlines()
        def index_searched(lines, substring):
            for i, s in enumerate(lines):
                if search_item in s:
                    i = lineNUM
                    linei[1] = i
                    break
            for i>lineNUM, s in enumerate(lines):
                if search_item in s:
                    linei[2] = i
            return -1

        for line in lines:
            if len(lines) > int(linei[1]):
                line = lines[linei[1]]
                previous = line[-1]
                #do stuff
             if len(lines) > int(linei[2]):
                line = lines[linei[2]]
                previous = line[-1]
                #do stuff

I am getting an error for the part where I try to save linei[i] in the first loop. I am trying to use that to create linei[1] and linei[2] which would give the line #s the search string is found in. I also assume that once I fix that, the way I tried to define the previous lines will give me an error. Any advice there?

jrwaller
  • 51
  • 11

1 Answers1

0

What is the problem you are actually trying to solve, could you post an example with generic data? This could almost certainly be accomplished more easily using regex or similar. You also probably do not want to be defining a function within while loops.

Example Regex Solution

import re

test_str = """hundreds of lines of text
    more lines

    previous line1
    search_term


    previous line2
    search_term

    more blocks of text"""

regex = r"(?P<prev_line>.*)\n(?P<match_line>.*search_term.*)"

matches = re.findall(regex, test_str)
for i, match in enumerate(matches, start=1):
    print(f'Match: {i}\n\tPrev Line: {match[0]}\n\tMatch Line: {match[1]}')

Example Output

Match: 1
        Prev Line:      previous line1
        Match Line:     search_term
Match: 2
        Prev Line:      previous line2
        Match Line:     search_term
CMMCD
  • 360
  • 1
  • 8
  • I edited it to include a simple text doc example. Thank you for the suggestion. – jrwaller Jul 18 '19 at 14:08
  • You should be able to use regex to solve this, maybe something like this `(?P.*)\n(?P.*search_term.*)` You can see an example of this [here](https://regex101.com/r/8MbDsp/1). – CMMCD Jul 18 '19 at 14:22
  • How would I use regex to differentiate between the first and second occurrences of "search_term"? – jrwaller Jul 18 '19 at 14:38
  • Matches will be returned in order, updated answer with code sample. – CMMCD Jul 18 '19 at 14:45
  • A couple comments on this response: I've updated my code to include your solution, and the outputs are not printing. Adding these lines of code also makes the code take around 10 minutes longer to run. Any thoughts here? – jrwaller Jul 18 '19 at 15:36
  • Without seeing your code I can't give you a specific answer, I would start by making sure you can run the code above alone then trying to integrate it with the rest of your program. – CMMCD Jul 18 '19 at 17:21