LYLlllhhh 发表于 2021-2-22 21:40:44

请大佬们看看这些错误都是什么

本帖最后由 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['内容'])+'\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

Stubborn 发表于 2021-2-23 00:06:12

请添加解码方式为GB18030:
df = pd.read_csv(r'qunaer-budalagong.csv', encoding='GB18030')

LYLlllhhh 发表于 2021-2-23 09:15:27

Stubborn 发表于 2021-2-23 00:06
请添加解码方式为GB18030:

嗯嗯好,解决啦。谢谢!
页: [1]
查看完整版本: 请大佬们看看这些错误都是什么