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

Question:

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.

Asked By: brice0408

||

Answers:

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"

Answered By: Christian Trujillo

You really don’t need regex if you know that ‘test’ appears in every line. Just slice the string at the index of were test begins plus the length of test

with open('locations.txt', 'r') as f, open('output.txt', 'w') as fo:
for line in f:
    fo.write(line[:line.index('test') + len('test')])
Answered By: Alexander

Have a look at split(). .split(separator, maxsplit) is going to slice the string at the keyword and append them to a list which is returned then returned.
Set the maxsplit to 1 if there are multiple occurences of the keyword but you only need the first one.

with open('locations.txt', 'r') as f, open('output.txt', 'w') as fo:
    for line in f:
        new_string = line.split('test')[0] + "test"#  split removes the separator keyword
        fo.write(new_string)
Answered By: Mole
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.