Searching specific string or word in a large textf without regex
Today, I will share my small experience how to create a small function that can do a quick job for
you in the case that you want to find a specific string or word in a large text
or in a book before going to show you on my next blog how to find a specific
string in sentence within a thousand or whatever number of books over there in
folder.
As always
cultivating in python, python stay always a big farmer ground where you can
cultivate whatever you want within a possibilities of its ground also. If you
can farm just go and farm whatever again you want.
Here a quick text that I’m going to work with today, let’s write some
text here:
text = “”” Python can be challenge for a lot of people whose daily
life is deep thinking on a possibilities of any project or anything around us.
Let say, we used to look on sky and sometime a possibilities to see a flight crossing
somewhere on a very straight line. I can think ooh yeah there must be a very
strong engines keeping such machine to flight easily. How many engines keeps
that’s machine flying, maybe one maybe two. Thinking to a possible risk and
asking a simple question. What’s going to happened on a flight running with one
engine that’s suddenly slowing down? Ooh yeah, thinking in philosophic of
python can suggest an idea, ooh yeah, is there a possibility to have another
engine on that flight which can be heated in a same heat of the essential
engine and keep running at least by the python code which can address a message
to the second machine, in the case of any feels (at least you have to inform
the engine what’s kind of feels and when I have to feel and when I have to take
a decision). I just can try first to tell the engine ooh yeah try to act
immediately in the case of any following cases within the condition of
functions I trained you before. That’s just a dead question for a machine
engineer who took already all those risks in consideration first, but we are
writing a text here just to analyze something in python. Anyway reflection to a
causation of any risks can also think to a solution”””
The goal here is to create a function in python that can locate a
specific string I’m looking and print me the sentence.
>>> def locating_string_in_sentence(word):
sp_text =
text.split()
sen_text =
text.split('.') # Sentence spliting without using splitlines
for w in
sencoranworktext:
if word in
w:
print(w.partition(word))
Let’s find ‘think’ in our function (but to be more efficient in
python in future, you must be able to find think only not thinking or thinks or
any derives at least you create your derives variable for specific verb).
>>> locating_string_in_sentence('think')
(' Python can be challenge for a lot of people whose daily life is
deep ', 'think', 'ing on a possibilities of any project or anything around us')
(' I can ', 'think', ' ooh yeah there must be a very strong engines
keeping such machine to flight easily')
(' What’s going to happened on a flight running with one engine
that’s suddenly slowing down? Ooh yeah, ', 'think', 'ing in philosophic of
python can suggest an idea, ooh yeah, is there a possibility to have another
engine on that flight which can be heated in a same heat of the essential
engine and keep running at least by the python code which can address a message
to the second machine, in the case of any feels (at least you have to inform
the engine what’s kind of feels and when I have to feel and when I have to take
a decision)')
(' Anyway reflection to a causation of any risks can also ', 'think', ' to a solution')
In the case you have a book with almost 2000 pages, printing ‘think’ will be too long, it’s better to print it into a specific text file and read it there without stacking your python shell.
>>> print(w.partition(word), file = open(‘think.txt’, ‘a+’,
encoding = ‘utf-8’))
Comments
Post a Comment