您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页python实现文本中单词个数统计

python实现文本中单词个数统计

来源:保捱科技网

from operator import itemgetter

file_name = "F:\\python.txt"
line_counts = 0
word_counts = 0
character_counts = 0
count = {}
word = ""

with open(file_name, 'r') as f:
    for line in f:
        words = line.split()
        for index,word in enumerate(words):
            if word in count:
                count[word] += 1
            else:
                count[word] = 1
        line_counts += 1
        word_counts += len(words)
        character_counts += len(line)
    for index, item in count.items():
        print("单词",index,"个数=",item)

print("行数 ", line_counts)
print("单词的个数 ", word_counts)
print("字节数 ", character_counts)


转载于:https://my.oschina.net/u/238361/blog/359944

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务