|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#数据读取
import pandas as pd
df=pd.read_csv('D:\\本人\\必须冲!\\学nlp\\安徽.csv') #此处路径需要修改
with open('实验.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('安徽.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 "D:/本人/必须冲!/学nlp/微博评论评分 - 可用.py", line 4, in <module>
df=pd.read_csv('D:\\本人\\必须冲!\\学nlp\\安徽.csv') #此处路径需要修改
File "C:\Users\lyl\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\io\parsers.py", line 610, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\lyl\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\io\parsers.py", line 468, in _read
return parser.read(nrows)
File "C:\Users\lyl\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\io\parsers.py", line 1057, in read
index, columns, col_dict = self._engine.read(nrows)
File "C:\Users\lyl\PycharmProjects\pythonProject1\venv\lib\site-packages\pandas\io\parsers.py", line 2061, in read
data = self._reader.read(nrows)
File "pandas\_libs\parsers.pyx", line 756, in pandas._libs.parsers.TextReader.read
File "pandas\_libs\parsers.pyx", line 771, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas\_libs\parsers.pyx", line 827, in pandas._libs.parsers.TextReader._read_rows
File "pandas\_libs\parsers.pyx", line 814, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas\_libs\parsers.pyx", line 1951, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 3, saw 2 |
|