用python对字符串分词,并计算词数

更新时间:2023-07-03 01:08:37 阅读: 评论:0

⽤python对字符串分词,并计算词数# -*- coding: utf-8 -*-
"""
Created on Tue Mar 24 11:29:53 2020
@author: weisssun
怎么折宝剑
"""
import jieba
import csv
import pandas as pd
jieba.load_urdict(r"D:\Python\")
#加载⽤户词典
input_path = r'D:\Python\data\评论.xlsx'
祝别人生日快乐#原始数据路径
outpath = r'D:\Python\data\评论-词数.csv'
#输出数据路径
results =[]
#将结果保存在results列表中,写⼊csv⽂件
raw_data = pd.read_excel(input_path, encoding ='utf-8')均匀拼音
#读取原始数据
for comment in t.astype(str):
鼻头长痣
#读取原始数据中的⽂本列,并将其转换为字符串格式
#否则jieba会报错
cut_words=[]
竹溪县
result =[]
#print(comment)
g_list = jieba.cut(comment,cut_all=Fal)
u盘读不出来怎么办#调⽤jieba分词
for i in g_list:
公司经营模式
cut_words.append(i)
#将分词结果保存为列表
#print(cut_words)
#print(len(cut_words))
#word_freq.append(len(cut_words))
result.append(comment)
result.append(cut_words)
result.append(len(cut_words))柏拉图的理想国
#将原始评论、分词结果、词数保存在列表result中
results.append(result)
#将所有result保存在列表results中
#print(results)
with open(outpath,'w', newline='',encoding='gbk')as f:
writer = csv.writer(f)
for result in results:
writer.writerow(result)
#将结果写⼊csv⽂件,每个result是⼀⾏

本文发布于:2023-07-03 01:08:37,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1065311.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:分词   结果   保存   路径   原始数据   列表   词数   字符串
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图