⽤python对字符串分词,并计算词数# -*- coding: utf-8 -*-
"""
beautiful girlCreated 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'esx
prior#输出数据路径
results =[]
#将结果保存在results列表中,写⼊csv⽂件
raw_data = pd.read_excel(input_path, encoding ='utf-8')
comfortably
#读取原始数据
for comment in t.astype(str):名古屋产业大学
#读取原始数据中的⽂本列,并将其转换为字符串格式
#否则jieba会报错
cut_words=[]
result =[]
#print(comment)
编程是什么课程
g_list = jieba.cut(comment,cut_all=Fal)
#调⽤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是⼀⾏analogy