For this program I am trying to figure out how to sort/print the words out based on frequency of appearance in ascending order. As of now I can only get the words to sort/print based on alaphabetical order
from collections import Counter
import string
while True:
filename=raw_input('Enter a file name: ')
if filename == 'exit':
break
try:
file = open(filename, 'r')
text=file.read()
file.close()
except:
print('file does not exist')
else:
for word in string.punctuation:
text=text.replace(word, "")
word_list = text.lower().split(None)
word_freq = {}
for word in word_list:
word_freq[word] = word_freq.get(word, 0) + 1
keys = sorted(word_freq.keys())
for word in keys:
if len(word) > 1:
print (word, word_freq[word])//the words print out based on alphabetic order
//how would I get them to print based on freqeuncy in ascending order?
print('Bye')