473,499 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check of two words are present in text file or not. Python script

1 New Member
The two words should be present or none should be present in text file. User should input two words and that word should be check into the text file be it any type such as .html,.txt,.doc,.php using python.
Nov 29 '18 #1
1 1221
Luuk
1,047 Recognized Expert Top Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. bla
  2. bla
  3. ble
  4. blu
  5. blo
  6. bla
The example says to do this to find 1 occurrence of your search text:
Expand|Select|Wrap|Line Numbers
  1. if 'bla' in open('example.txt').read():
  2.     print("true")
  3.  
This seems to work:
Expand|Select|Wrap|Line Numbers
  1. C:\temp>python
  2. ActivePython 3.5.3.3505 (ActiveState Software Inc.) based on
  3. Python 3.5.3 (default, May 16 2017, 01:12:46) [MSC v.1900 64 bit (AMD64)] on win32
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> if "bla" in open("example.txt").read():
  6. ...   print("true")
  7. ...
  8. true
  9. >>> if "blx" in open("example.txt").read():
  10. ...   print("true")
  11. ...
  12. >>>
  13.  
Let's first create a script with this example. Store the following in a text-file ame 'search.py'
Expand|Select|Wrap|Line Numbers
  1. if "bla" in open("example.txt").read():
  2.    print("true")
  3.  
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:
Expand|Select|Wrap|Line Numbers
  1. if "bla" in open("example.txt").read():
  2.    print("true")
  3.  
  4. a = open("example.txt").read()
  5. print(a.count("bla"))
  6.  
When typing: python search.py
The answer should be:
Expand|Select|Wrap|Line Numbers
  1. true
  2. 3
(providing you followed above instructions ;)

This should help you on your way to find the complete solution to your question.
Dec 1 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
13764
by: Noam Dekers | last post by:
Hi all, I would like to find a word stored in a text file. Structure: I have one file named keyWords.txt that stores some key words I'm interested in finding. In addition I also have a file...
0
1296
by: pchahar | last post by:
Write a program to process a text file. The program will determine how many unique words there are in the text file that begin with each letter of the alphabet. The text file name will be given as a...
7
3304
by: jimmyjamz | last post by:
So far I have it so that I can open up a file. From there I'm trying to go through the each word of the file and if it starts with an A, I want to throw it into a linked list for words starting with...
7
22112
by: elnoire | last post by:
Greetings! I've just started learning python, so this is probably one of those obvious questions newbies ask. Is there any way in python to check if a text file is blank? What I've tried to...
10
20894
by: klharding | last post by:
I am reading the contents of a text file into variables. All works well if the text file does not exist, or it does exist and contains 4 rows of data. But I need to account for rows being empty or...
2
1702
by: shane12345 | last post by:
i hav a text file anagrams.text.......and i hav a 2d array r....i want to search in the text file anagrams.txt the words present in the 2d array and if present print them....thanx....(im trying to...
4
20842
by: Nawaf Ali | last post by:
I am trying to do some text statistics, like word frequency, average word length, average sentence length, and average paragraph length, I managed to do the word frequency and the average sentence...
4
7110
by: scharnisto | last post by:
Hi, i need a script, that deletes all the lines between two certain words in a text file. the two words appear several times in the text file. i have a code, that works so far. the problem is,...
4
3773
by: Aplus212 | last post by:
Hi guys, Very new to Python and was hoping you guys could give me some help. I have a book about The Great War, and want to count the times a country appears in the book. So far i have this: ...
2
1737
by: dafrancesko | last post by:
Hi, I would be glad if you would help me with this program: Task: User will enter 4-7 letters (for example 'ADFG'). I have detached text file which contains about several thousand of words
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.