對cvs文本關鍵詞進行詞頻統計

#-*-coding:utf-8-*-
import os
import json
data={}
with open('2009_all_keywords.txt','r',encoding='utf-8',errors='ignore') as f:
    txt_read = f.read()
    txt_read= txt_read.strip()
    txt = txt_read.split('\n')
    for row in txt:
        list_row=row.split(' ')
        list_row = [i for i in list_row if(len(str(i))!=0)]
        for word in list_row:
            word=word.strip().strip(';')
            if word in data.keys():
                data[word]=data[word]+1
            else:
                data.update({word:1})
        print('********')
        data_print = sorted(data.items(),key=lambda item:item[1],reverse=True )
        print(data_print)
        #print('********')
data_save=sorted(data.items(),key=lambda item:item[1],reverse=True )
lsobj = json.dumps(data_save)
with open('2009_word_count.txt','w',encoding='utf-8',errors='ignore') as f:
    f.write(lsobj)

作者:WangB

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章