I am trying to read from a txt file and counts the number of times each word appears. The problem is that it counts the EOL characters as well. I want to skip them. I tried to use the rstrip, still it didn't do anything. So how can I handle these end-of-line characters?
I am using python 3.
Please help.
I am using python 3.
Please help.
Code:
Object= open('w.txt','r')
L= Object.read().rstrip()
occurrenences={}
for word in L.split():
occurrenences[word] = occurrenences.get(word,0)+1
for word in occurrenences:
print(occurrenences[word],word)
Object.close()
Comment