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.
CodePudding user response:
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"
CodePudding user response:
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')])
CodePudding user response:
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)