1

I am writing a code in python where I am removing all the text after a specific word but in output lines are missing. I have a text file in unicode which have 3 lines:

my name is test1
my name is
my name is test 2

What I want is to remove text after word "test" so I could get the output as below

my name is test
my name is
my name is test

I have written a code but it does the task but also removes the second line "my name is" My code is below

txt = ""
with open(r"test.txt", 'r') as fp:
    for line in fp.readlines():
        splitStr = "test"
        index = line.find(splitStr)
        if index > 0:
            txt += line[:index + len(splitStr)] + "\n"
with open(r"test.txt", "w") as fp:
    fp.write(txt)
3
  • You may need to add an else block. The second line does not have the word 'test'. Commented Apr 1, 2022 at 9:36
  • Could you please suggest how to use else in this scenario ? Commented Apr 1, 2022 at 9:38
  • When the substring is not in the string, you're not adding that line to the output. Also, this could be done in an easier way with txt += line.strip().split(splitStr)[0], avoiding unnecessary ifs Commented Apr 1, 2022 at 9:39

4 Answers 4

2

Your code does not append the line if the splitStr is not defined.

txt = ""
with open(r"test.txt", 'r') as fp:
for line in fp.readlines():
    splitStr = "test"
    index = line.find(splitStr)
    if index != -1:
        txt += line[:index + len(splitStr)] + "\n"
    else:
        txt += line
with open(r"test.txt", "w") as fp:
    fp.write(txt)
Sign up to request clarification or add additional context in comments.

3 Comments

what if find returns 0 (element found at the beginning)? you are then not removing what's after the split element but appending the whole line instead. try with the string "test hello". it should return "test" but it returns "test hello"
I don't think we need a new line (\\n) in the else block. The line itself has that. This code will work but writes an extra line in output file.
Corrected the code with the suggestions
0

It looks like if there is no keyword found the index become -1. So you are avoiding the lines w/o keyword. I would modify your if by adding the condition as follows:

txt = ""
with open(r"test.txt", 'r') as fp:
    for line in fp.readlines():
        splitStr = "test"
        index = line.find(splitStr)
        if index > 0:
            txt += line[:index + len(splitStr)] + "\n"
        elif index < 0:
            txt += line 
with open(r"test.txt", "w") as fp:
    fp.write(txt)

No need to add \n because the line already contains it.

1 Comment

how is this answer different from Mohamad Kamar one? Also you are missing the case where index is 0
0

In my solution I simulate the input file via io.StringIO. Compared to your code my solution remove the else branch and only use one += operater. Also splitStr is set only one time and not on each iteration. This makes the code more clear and reduces possible errore sources.

import io

# simulates a file for this example
the_file = io.StringIO("""my name is test1
my name is
my name is test 2""")

txt = ""
splitStr = "test"

with the_file as fp:
    # each line
    for line in fp.readlines():
        # cut somoething?
        if splitStr in line:
            # find index
            index = line.find(splitStr)
            # cut after 'splitStr' and add newline
            line = line[:index + len(splitStr)] + "\n"

        # append line to output
        txt += line

print(txt)

When handling with files in Python 3 it is recommended to use pathlib for that like this.

import pathlib
file_path = pathlib.Path("test.txt")

# read from wile
with file_path.open('r') as fp:
    # do something

# write back to the file
with file_path.open('w') as fp:
    # do something

Comments

-1

Suggestion:

for line in fp.readlines():
     i = line.find('test')
     if i != -1:
         line = line[:i]

4 Comments

when not found and find returns -1 your code will do line = line[:-1]. Is this an intended behaviour?
That would cut off the last character in the line, so I agree it is unintended and thank you for the suggestion. A way to sort this out would be to put line = line[:i] into an if block: if i != -1: I'll amend my suggestion accordingly.
this now removes the whole line if the split element is not found, which is the problem the OP is facing. Also if the element is found it removes the splitting element, but what should be removed s whatever is after the split element
Thankyou everyone. I got the solution Thanks again everyone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.