|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 LYLlllhhh 于 2021-2-22 22:14 编辑
- #数据读取
- import pandas as pd
- df=pd.read_csv('D:\\本人\\必须冲!\\学nlp\\qunaer-budalagong.csv') #此处路径需要修改
- with open('实验2.txt','w+',encoding='utf-8') as f:
- for i in range(len(df)):
- f.write(str(df['内容'][i])+'\n')
- import codecs
- import re
- import numpy as np
- import pymysql
- from snownlp import SnowNLP
- import matplotlib.pyplot as plt
- from snownlp import sentiment
- from snownlp.sentiment import Sentiment
-
- comment = []
- with open('实验2.txt', mode='r', encoding='utf-8') as f:
- rows = f.readlines()
- for row in rows:
- if row not in comment:
- comment.append(row.strip('\n'))
- key=[]
- values=[]
- re=[]
- def snowanalysis(self):
- sentimentslist = []
- for li in self:
- #text = re.sub(r'(?:回复)?(?://)?@[\w\u2E80-\u9FFF]+:?|\[\w+\]', ',',li)
- s = SnowNLP(li)
-
- key.append(li)
- values.append(s.sentiments)
- sentimentslist.append(s.sentiments)
- plt.figure(figsize=(12,8))
- plt.hist(sentimentslist, bins=np.arange(0, 1, 0.01))
- plt.show()
-
- snowanalysis(comment)
- res=pd.DataFrame({'评论':key,'评分':values})
- #保存文件为excel格式
- res.to_excel('D:\\本人\\必须冲!\\学nlp\\爱情.xlsx') #此处路径需要修改�
复制代码 学姐做成功过,用学姐给的文档可以出结果,但是用自己做的文档就是不行,请帮忙看看,需要怎么改啊
C:\Users\lyl\PycharmProjects\pythonProject1\venv\Scripts\python.exe "D:/本人/必须冲!/学nlp/微博评论评分 - 副本.py"
Traceback (most recent call last):
File "C:\Users\lyl\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '内容'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:/本人/必须冲!/学nlp/微博评论评分 - 副本.py", line 8, in <module>
f.write(str(df['内容'])+'\n')
File "C:\Users\lyl\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\core\frame.py", line 3024, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\lyl\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
raise KeyError(key) from err
KeyError: '内容'
Process finished with exit code 1
|
|