import os
def count_words(inputname):
fh = open(inputname)
read_fh = fh.read()
number = 1
is_alpha = []
dict_words = {}
#清洗数据
for word in read_fh:
if word.isalpha():
is_alpha.append(word)
elif word == '\t' or word == '\n' or word == ' ':
is_alpha.append(word)
#将数据由List转成字符串
fh_alpha = ''.join(is_alpha)
fh_words = fh_alpha.split()
#将字符串中的单词转成小写
for words in fh_words:
words = words.lower()
#统计每个单词的出现次数,并记录到字典当中
if words not in dict_words:
dict_words[words] = number
else:
dict_words[words] = dict_words[words] + 1
#打印单词字典(每个单词的出现次数)
print dict_words
count_words("dict.txt")