3、编写程序,对文件“天龙八部-网络版.txt“中出现的中文词语进行统计,采用jieba库分词,词语与出现次数之间用冒号:分隔,输出保存到“天龙八部-词语统计.txt“文件中。注意,不统计空格和回车字

3、编写程序,对文件"天龙八部-网络版.txt"中出现的中文词语进行统计,采用jieba库分词,词语与出现次数之间用冒号:分隔,输出保存到"天龙八部-词语统计.txt"文件中。注意,不统计空格和回车字符。存储格式如下:
天龙八部:10,作者:1,金庸:1

import jieba
fi = open("天龙八部-网络版.txt", "r", encoding='utf-8')
fo = open("天龙八部-词语统计.txt", "w", encoding='utf-8')
txt = fi.read()
words = jieba.lcut(txt)
d = {}
for w in words:
    if len(w) == 1:
        continue
    else:
        d[w] = d.get(w,0) + 1
ls = []
for key in d:
    ls.append("{}:{}".format(key, d[key]))
fo.write(",".join(ls))
fi.close()
fo.close()

你可能感兴趣的:(python)