One of the
answers give in the internet seems to be correct, so I will try to show hoe you can test this yourself.
Create some textfile (I will name it 'example.txt'), with the following content:
The example says to do this to find 1 occurrence of your search text:
- if 'bla' in open('example.txt').read():
-
print("true")
-
This seems to work:
- C:\temp>python
-
ActivePython 3.5.3.3505 (ActiveState Software Inc.) based on
-
Python 3.5.3 (default, May 16 2017, 01:12:46) [MSC v.1900 64 bit (AMD64)] on win32
-
Type "help", "copyright", "credits" or "license" for more information.
-
>>> if "bla" in open("example.txt").read():
-
... print("true")
-
...
-
true
-
>>> if "blx" in open("example.txt").read():
-
... print("true")
-
...
-
>>>
-
Let's first create a script with this example. Store the following in a text-file ame 'search.py'
- if "bla" in open("example.txt").read():
-
print("true")
-
and run: python search.py
you should get the result saying: true
Now, how to find if that word exists two (or more) times in example.txt?
Google for "find number of occurrences in a text in a string"
and change search.py to:
- if "bla" in open("example.txt").read():
-
print("true")
-
-
a = open("example.txt").read()
-
print(a.count("bla"))
-
When typing: python search.py
The answer should be:
(providing you followed above instructions ;)
This should help you on your way to find the complete solution to your question.