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
You can simply use find
method of str
class which tells you the lowest index where substring is found.
I modified your code to something like this:
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
for subline in line.split('.'):
word_index = subline.find(string_to_search)
if word_index != -1:
results.append(subline[word_index:].strip('\n') + '.')
return results
print(search_string_in_file(a, b))
Now, It can handle exactly what you want.
Alternatively you could just print the line instead of storing it into a list in case the txt file is big enough and you don't want to store the lines.
btw, you haven't use line_number
variable anywhere!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.