Home > Back-end >  Lines missing in python
Lines missing in python

Time:04-01

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)

CodePudding user response:

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)

CodePudding user response:

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.

CodePudding user response:

Suggestion:

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

CodePudding user response:

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
  • Related