python3.3.2 如何统计文本文件中出现的每个单词数目,单词之间使用空格隔开
如下面这段英文
test.txt里面包含以下内容
hello feel love I you test test one two three
worlds
Pursuing further research may involve at a subsequent stage
the acquisition of a doctoral degree hello feel love I you test test one two three
Pursuing further research may involve
at a subsequent stage
the acquisition of a doctoral degree
如何统计这个文件中的每个单词出现的次数
如何将结果写入到文件中呢?
追答python 2.x将最后的print语句改为
print >>result.txt,"%s:%d"%(word,countdict[word])
python 3.x版本,则改为
print("%s:%d"%(word,countdict[word]),file=open("result.txt"))
其中result.txt为要写入的文件