鱼C论坛

 找回密码
 立即注册
查看: 2141|回复: 3

求问!如何用python逐条导入excel的文本,然后用结巴分词进行情感色彩分析

[复制链接]
发表于 2018-10-7 20:19:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我有一个新闻列表的excel文件,有100000条数据。有一列是新闻的内容。如何用python逐条导入该列文本内容,并用结巴分词进行情感色彩分析,并将分析结果(积极或消极)放到新的一列“情感色彩”中。
1538914690(1).png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-7 20:37:29 | 显示全部楼层
嗯哼。。。
读取可以用openpyxl库:
https://www.cnblogs.com/sun-haiyu/p/7096423.html这里有个简易教程,论坛里应该也有个比较全的教程。
jieba分词情感分析。。你的意思是自己train一个model嘛,还是用类似snowNLP这些现成的库,虽然from snownlp import sentiment,也可以train。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-7 20:39:26 | 显示全部楼层
没数据,没法写代码。。。你可以自己先试试,然后追问。。。或者把数据发一部分给我。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-7 20:51:45 | 显示全部楼层
可以用pywin32模块,下面是网上封装的一些代码。
  1. from win32com.client import Dispatch
  2. import win32com.client


  3. class EasyExcel:
  4.     """A utility to make it easier to get at Excel.    Remembering  
  5.     to save the data is your problem, as is    error handling.  
  6.     Operates on one workbook at a time."""

  7.     def __init__(self, filename=None):  # 打开文件或者新建文件(如果不存在的话)  
  8.         self.xlApp = win32com.client.Dispatch('Excel.Application')
  9.         if filename:
  10.             self.filename = filename
  11.             self.xlBook = self.xlApp.Workbooks.Open(filename)
  12.         else:
  13.             self.xlBook = self.xlApp.Workbooks.Add()
  14.             self.filename = ''

  15.     def save(self, newfilename=None):  # 保存文件  
  16.         if newfilename:
  17.             self.filename = newfilename
  18.             self.xlBook.SaveAs(newfilename)
  19.         else:
  20.             self.xlBook.Save()

  21.     def close(self):  # 关闭文件  
  22.         self.xlBook.Close(SaveChanges=0)
  23.         del self.xlApp

  24.     def getCell(self, sheet, row, col):  # 获取单元格的数据  
  25.         """Get value of one cell"""
  26.         sht = self.xlBook.Worksheets(sheet)
  27.         return sht.Cells(row, col).Value

  28.     def setCell(self, sheet, row, col, value):  # 设置单元格的数据  
  29.         """set value of one cell"""
  30.         sht = self.xlBook.Worksheets(sheet)
  31.         sht.Cells(row, col).Value = value

  32.     def setCellformat(self, sheet, row, col):  # 设置单元格的数据  
  33.         """set value of one cell"""
  34.         sht = self.xlBook.Worksheets(sheet)
  35.         sht.Cells(row, col).Font.Size = 15  # 字体大小  
  36.         sht.Cells(row, col).Font.Bold = True  # 是否黑体  
  37.         sht.Cells(row, col).Name = "Arial"  # 字体类型  
  38.         sht.Cells(row, col).Interior.ColorIndex = 3  # 表格背景  
  39.         # sht.Range("A1").Borders.LineStyle = xlDouble  
  40.         sht.Cells(row, col).BorderAround(1, 4)  # 表格边框  
  41.         sht.Rows(3).RowHeight = 30  # 行高  
  42.         sht.Cells(row, col).HorizontalAlignment = -4131  # 水平居中xlCenter  
  43.         sht.Cells(row, col).VerticalAlignment = -4160  #

  44.     def deleteRow(self, sheet, row):
  45.         sht = self.xlBook.Worksheets(sheet)
  46.         sht.Rows(row).Delete()  # 删除行  
  47.         sht.Columns(row).Delete()  # 删除列

  48.     def getRange(self, sheet, row1, col1, row2, col2):  # 获得一块区域的数据,返回为一个二维元组  
  49.         """return a 2d array (i.e. tuple of tuples)"""
  50.         sht = self.xlBook.Worksheets(sheet)
  51.         return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value

  52.     def addPicture(self, sheet, pictureName, Left, Top, Width, Height):  # 插入图片  
  53.         """Insert a picture in sheet"""
  54.         sht = self.xlBook.Worksheets(sheet)
  55.         sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height)

  56.     def cpSheet(self, before):  # 复制工作表  
  57.         """copy sheet"""
  58.         shts = self.xlBook.Worksheets
  59.         shts(1).Copy(None, shts(1))

  60.     def inserRow(self, sheet, row):
  61.         sht = self.xlBook.Worksheets(sheet)
  62.         sht.Rows(row).Insert(1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-2 16:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表