Python写入文件

时间:2013-11-15 19:20:06

标签: python

您好我正在制作打印独特单词和频率的程序,我发现下面的代码...只有一块是我需要将它写入我本地驱动器上的文件...有人可以帮助我感谢

import re
filename = 'C:\pythonscripts\comments.txt'
word_list = re.split('\s+', file(filename).read().lower())
print 'Words in text:', len(word_list)
freq_dic = {}
punctuation = re.compile(r'[.?!,":;]')
for word in word_list:

    word = punctuation.sub("", word)
    try:
        freq_dic[word] += 1
    except:
        freq_dic[word] = 1
print 'Unique words:', len(freq_dic)
freq_list = freq_dic.items()
freq_list.sort()

for word, freq in freq_list:
     print word, freq

0 个答案:

没有答案