python练习 0004

来源:互联网 发布:淘宝女装店铺改卖男装 编辑:程序博客网 时间:2024/06/09 18:04

第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数。

思路:

1.打开文本文件

2.读取文本文件内容,去除换行符以及按照空格和逗号分隔将单词加入list中

3.使用collections中Counter来统计单词个数

import collectionsimport redef calwords(path):    word = []    with open(path) as file:        data = file.readlines()    for line in data:        word += re.split(' |,',line.strip('\n'))    print(collections.Counter(word))if __name__ == '__main__':    calwords('e://code.txt')


原创粉丝点击