import sys
def candidateWord():
filePath = "sample.txt"
file = open(filePath,'r')
word_count = {}
for line in sys.stdin.readlines():
for word in line.split():
#words = word.lower()
words = word.strip('!,.?1234567890-=@#$%^&*()_+').lower()
word_count[words] = word_count.get(words,0) + 1
for key in word_count.keys():
#sorted(word, key = str,lower)
print (str(key)+' '+str(word_count[key]))
candidateWord()
How would I sort the words I have in a textfile by their frequency using what I have already?
The text file (sample.txt) contains the following: How are you How are you I am good. HBHJKOLDSA How
My desire output should be:
how 3
am 2
are 2
i 2
you 2
good 1
hbhjkoldsa 1
I am working in python 3.