Skip to content Skip to sidebar Skip to footer

Add New Line After Finding Last String In A Region

I have this input test.txt file with the output interleaved as #Expected in it (after finding the last line containing 1 1 1 1 within a *Title region and this code in Python 3.6 in

Solution 1:

Because you are already reading the entire file into memory anyway, it's easy to scan through the lines twice; once to find the last transition out of a region after each title, and once to write the modified data back to the same filename, overwriting the previous contents.

I'm introducing a dictionary variable transitions where the keys are the indices of the lines which have a transition, and the value for each is the text to add at that point.

transitions = dict()
in_region = False
reg_end = -1
current_title = Nonewithopen("test.txt","r") as testfile:
    content = testfile.readlines()

for idx, line inenumerate(content):
    if line.startswith('*Title '):
        # Commit last transition before this to dict, if anyif current_title:
            transitions[reg_end] = current_title
        # add suffix for printing
        current_title = line.rstrip('\n') + '2\n'elif line.strip().endswith(' 1 1 1 1'):
        in_region = True# This will be overwritten while we remain in the region
        reg_end = idx
    elif in_region:
        in_region = Falseif current_title:
    transitions[reg_end] = current_title

withopen("test.txt", "w") as output:
    for idx, line inenumerate(content):
        output.write(line)
        if idx in transitions:
            output.write(transitions[idx])

This kind of "remember the last time we saw something" loop is very common, but takes some time getting used to. Inside the loop, keep in mind that we are looping over all the lines, and remembering some things we saw during a previous iteration of this loop. (Forgetting the last thing you were supposed to remember when you are finally out of the loop is also a very common bug!)

The strip() before we look for 1 1 1 1 normalizes the input by removing any surrounding whitespace. You could do other kinds of normalizations, too; normalizing your data is another very common technique for simplifying your logic.

Demo: https://ideone.com/GzNUA5

Solution 2:

try this, using itertools.zip_longest

from itertools import zip_longest

withopen("test.txt","r") as f:
    content = f.readlines()

results, title = [], ""for i, j in zip_longest(content, content[1:]):
    # extract title.if i.startswith("*"):
        title = i

    results.append(i)

    # compare value in i'th index with i+1'th (if mismatch add title)if"1 1 1 1"in i and"1 1 1 1"notin j:
        results.append(f'{title.strip()}2\n')

print("".join(results))

Post a Comment for "Add New Line After Finding Last String In A Region"