Discuss / Python / 统计文本英文每个单词出现的次数

统计文本英文每个单词出现的次数

Topic source
import string

f = open(r"test.txt",'r')
file = f.read().split()
def add(word,word_dict):
    if word in word_dict:
        word_dict[word]+=1
    else:
        word_dict[word]=1



s = ["".join([x for x in i if x not in string.punctuation+"“”, 。-"]) for i in file]





o = 0
word_dict = {}
for i in s:
    add(i,word_dict)

for x,y in sorted(word_dict.items()):
    o+=1
    print("%+2s %-20s %s" % (o,x,y),)
print("Total: %s " % o)

  • 1

Reply