代码
"""
Spyder Editor
This is a temporary script file.
"""
import xlrd
import numpy as np
import pandas as pd
import jieba
import collections
import xlwt
def readexcel(rPath):
workbook = xlrd.open_workbook(rPath)
sheet = workbook.sheet_by_index(1)
ylist = []
for i in range(1, sheet.nrows):
ylist.append(str(sheet.cell_value(rowx = i, colx = 1)))
return ylist
def countWord(l):
l = ''.join(l)
l = list(jieba.lcut(l))
word_counts = dict(collections.Counter(l))
word_counts=dict(sorted(word_counts.items(), key=lambda x:x[1], reverse=True))
print(word_counts)
return word_counts
def write2Excel(wPath, word_counts):
workbook = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('Sheet1')
worksheet.write(0,0, '词汇')
worksheet.write(0,1, '频率')
row=1
for k,v in word_counts.items():
worksheet.write(row,0, k)
worksheet.write(row,1, v)
row+=1
workbook.save(wPath)
return
rPath = r'C:\Users\K43\Desktop\xppcode\热词统计.xlsx'
wPath = r'C:\Users\K43\Desktop\xppcode\热词统计-结果.xlsx'
ylist = readexcel(rPath)
result= countWord(ylist)
write2Excel(wPath, result)