文本的行数,字符的总数,单词的数量和出现的频率,特殊字符的统计。
# -*- cofing: utf-8 -*-
import os
import sys
import collections
import string
script_name = sys.argv[0]
res = {
"total_lines":"",
"total_characters":"",
"total_words":"",
"unique_words":"",
"special_characters":""
}
try:
textfile = sys.argv[1]
with open(textfile, "r", encoding = "utf_8") as f:
data = f.read()
res["total_lines"] = data.count(os.linesep)
res["total_characters"] = len(data.replace(" ","")) - res["total_lines"]
counter = collections.Counter(data.split())
d = counter.most_common()
res["total_words"] = sum([i[1] for i in d])
res["unique_words"] = len([i[0] for i in d])
special_chars = string.punctuation
res["special_characters"] = sum(v for k, v in collections.Counter(data).items() if k in special_chars)
except IndexError:
print('Usage: %s TEXTFILE' % script_name)
except IOError:
print('"%s" cannot be opened.' % textfile)
print(res)

最后
以上就是精明猫咪最近收集整理的关于Python版的迷你程序——文本内容的简单统计分析的全部内容,更多相关Python版内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复