python 词频统计并将结果写入一个txt中、如何写_python 统计词频后输出结果到Excel和txt文档...

分词的应用场景还是蛮多的,比如电商里面的产品用户评价,每个商品的评价数很多,由于是大量的文本,需要从这些文本找出评价的关键字,这个时候分词就能派上用场;再比如,做文本分析,热点词统计都会用到分词。

整体思路就是利用python中的jieba库,对每一行文字进行分词处理,处理之后的结果放在list(列表)中。遍历列表,重复的值就计数,留下唯一的值作为key。

引用库

import jieba #分词

import xlwt #Excel库

import pandas as pd #pandas库

import matplotlib as mpl #matplotlib库由各种可视化类构成

from wordcloud import WordCloud #词云

import matplotlib.pyplot as plt #matplotlib.pyplot是绘制各类可视化图形的命令字库,相当于快捷方式

配置

mpl.rcParams["font.sans-serif"] = ['Microsoft YaHei'] #配置字体

plt.rcParams["axes.labelsize"] = 16 #轴域大小

plt.rcParams["xtick.labelsize"] =14 #x轴字体大小

plt.rcParams["ytick.labelsize"] =14 #y轴字体大小

plt.rcParams["legend.fontsize"]=12 #图例字体大小

plt.rcParams["figure.fig

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用Python批量对txt文件进行去除停用词,分词和统计词频的详细代码,并将结果输出到Excel的步骤: 首先需要安装以下Python库: - jieba(用于文分词) - openpyxl(用于操作Excel文件) 可以通过以下命令进行安装: ``` pip install jieba openpyxl ``` 接下来是代码部分,注释已加入代码,直接运行即可: ```python import os import jieba import openpyxl from openpyxl.utils import get_column_letter from openpyxl.styles import Font # 停用词文件路径 stopwords_path = "stopwords.txt" # 获取停用词列表 stopwords_list = [] with open(stopwords_path, "r", encoding="utf-8") as f: for line in f: stopwords_list.append(line.strip()) # 创建工作簿和工作表 workbook = openpyxl.Workbook() worksheet = workbook.active # 设置表头 worksheet["A1"] = "文件名" worksheet["B1"] = "词语" worksheet["C1"] = "词频" # 设置表头样式 for col in range(1, 4): cell = worksheet.cell(row=1, column=col) cell.font = Font(name="宋体", size=12, bold=True) cell.alignment = openpyxl.styles.Alignment(horizontal="center", vertical="center") # 获取所有txt文件 txt_files = [filename for filename in os.listdir() if filename.endswith(".txt")] # 遍历所有txt文件 for index, filename in enumerate(txt_files): # 读取文件内容 with open(filename, "r", encoding="utf-8") as f: content = f.read() # 分词并去除停用词 words = jieba.cut(content) words_list = [word for word in words if word not in stopwords_list] # 统计词频 words_count = {} for word in words_list: if word in words_count: words_count[word] += 1 else: words_count[word] = 1 # 将文件名、词语和词频写入Excel for i, (word, count) in enumerate(words_count.items()): worksheet.cell(row=index+2+i, column=1, value=filename) worksheet.cell(row=index+2+i, column=2, value=word) worksheet.cell(row=index+2+i, column=3, value=count) # 自适应列宽 for col in worksheet.columns: max_length = 0 column = get_column_letter(col[0].column) for cell in col: try: if len(str(cell.value)) > max_length: max_length = len(str(cell.value)) except: pass adjusted_width = (max_length + 2) worksheet.column_dimensions[column].width = adjusted_width # 保存Excel文件 workbook.save("word_frequency.xlsx") ``` 这段代码会在当前文件夹读取所有以`.txt`结尾的文件,对每个文件进行分词、去除停用词、统计词频,并将结果输出到一个名为`word_frequency.xlsx`的Excel文件。其,第一列为文件名,第二列为词语,第三列为词频。 需要注意的是,由于这段代码使用了文分词,因此需要保证停用词文件和待处理的txt文件编码均为UTF-8。另外,由于分词的准确性和词频统计的效果与具体的业务场景和语料库有关,因此需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值