Write a python script to read the data from a text file India t XT find and display the occurrence of the word " India" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a python script to read the data from a text file India t XT find and display the occurrence of the word " India"

13th Apr 2021, 3:26 PM
Himanshi Tilwani
Himanshi Tilwani - avatar
3 Answers
+ 1
This might not count correctly if the word India appears more than once on a given line. In your code, you are only counting lines with the word India, not the number of times the word India occurs.
13th Apr 2021, 6:37 PM
Jerry Hobby
Jerry Hobby - avatar
+ 1
Jerry Hobby Thanks for pointing that out Sir. Himanshi Tilwani with open("filename.txt") as f: file_list = f.readlines() count = 0 for line in file_list: line = line.split() for word in line: if word== "India": count+=1 print(count)
13th Apr 2021, 7:49 PM
CHANDAN ROY
CHANDAN ROY - avatar
0
Himanshi Tilwani with open("filename.txt") as f: file_list = f.readlines() count = 0 for line in file_list: if "India" in line: count +=1 print(count)
13th Apr 2021, 4:14 PM
CHANDAN ROY
CHANDAN ROY - avatar