This is a program to find the number of occurrence of a particular word using python.We read the contents of the file into a list and use the count function to get the count of words.
#/usr/bin/python
file = open("/home/Documents/alice.txt","r")
word = file.read().split()
print word
print word.count("Alice")
Here I have read the chapter 1 of story Alice in Wonderland by Lewis Carroll taken from the site www.gutenberg.org.
The file is read in read mode and it is converted into a list called "word".The split() function will consider a space as default delimiter for split if nothing is specified. We have function like count() which can be used to apply directly on a list.Here the word which need to be searched "Alice" is given as argument.
Comments
Post a Comment