Skip to content
Advertisement

Python, print from keyword to next dot

So this is my code

a = input("Enter file name: ")
b = input("Enter keyword: ")

def search_string_in_file(file_name, string_to_search):
    line_number = 0
    results = ""
    with open(file_name, 'r' , encoding='latin1') as read_obj:
        for line in read_obj:
            line_number += 1
            if string_to_search in line:
                print(line)

search_string_in_file(a, b)

At the moment it opens the file which you are setting in the first input and searches this file line by line for the keyword you set on the second input.

As it is now it prints the whole line where the keyword was found.

What I wanna do is just to print from the keyword onwards to the next dot.

For example: file.txt

This is my house. I like it.
But my girlfriend hates it, but that's ok.

keyword = my

The actual result prints both lines because both lines contain “my”. But it only should print this:

my house.
my girlfriend hates it, but that's ok.

Didn’t find any answer so far, please help me

Advertisement

Answer

We can splice into the string line by using the operator []. With the help of str.find(), we can determine the little portion we need to print. From the documentation:

The find() method returns the index of first occurrence of the
substring (if found). If not found, it returns -1.

So here’s how we could rewrite the code:

a = input("Enter file name: ")
b = input("Enter keyword: ")

def search_string_in_file(file_name, string_to_search):
    line_number = 0
    results = ""
    with open(file_name, 'r' , encoding='latin1') as read_obj:
        for line in read_obj:
            line_number += 1
            word_index = line.find(string_to_search)  # position of first letter of the word
            if (word_index != -1):  # meaning the word was found
                period_index = line.find('.', word_index)  # position of first period after start of word
                print(line[word_index:period_index]

search_string_in_file(a, b)

Keep in mind that this will get wonky if there is a period ‘.’ inside the string_to_search. To make sure you print out the whole string in this case, do this instead:

period_index = line.find('.', word_index+len(string_to_search))

This skips the whole length of string_to_search before looking for periods.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement