python 計算txt文字詞頻率

zsdeus133發表於2018-07-29

如: “123—-123, 567—-567, ……..” (txt要事先轉換為utf-8)
參考 http://www.docin.com/p-1722000891.html

import re
from collections import Counter

with open('1 (2).txt') as f:
    pass_list = f.read().split("\n")

for i in range(0, len(pass_list)):
    r = re.search("----(.*?)$", pass_list[i])
    if r is not None:
        pass_list[i] = r.group(1)
    else:
        pass_list[i] = ""

c = Counter(pass_list).most_common(20)
for each in c:
    print(each)

相關文章