Search code examples
pythonstringfiletextreplace

Python: How to remove part of a string starting at a keyword for multiple lines?


Here is my code:

with open('locations.txt', 'r') as f, open('output.txt', 'w') as fo:
for line in f:
    fo.write(line.replace('test'[:-1], ''))

I have a file with multiple lines of text:

This is a test the cat jumped around
This is another test the dog jumped under
This is a third test the cow jumped over

I want to be able to open the text file, and remove everything on each line after the word 'test'. So result would look like:

This is a test
This is another test
This is a third test

I am trying to use .replace() but using the paramter of -1 it just removed everything but the last letter in test. I am really not sure how to have the word 'test' be the input then have it remove the rest of the string after that on each line.


Solution

  • use regex to find where "test" first appears in your string

    with open('locations.txt', 'r') as f, open('output.txt', 'w') as fo:
        for line in f:
            index = re.search("test",line).span()[1]
            fo.write(line[:index ])
    

    heres a breakdown:

    re.search("test",line) searches for "test" in line

    re.search("test",line).span() returns a tuple with the starting position and the ending position of what you wanted to find ("test")

    re.search("test",line).span()[1] gives you the ending position of the word "test" in the line

    finally line[:index ] gives you a slice of line up until the ending position of where it found "test"