鱼C论坛

 找回密码
 立即注册
查看: 1740|回复: 1

想麻烦大佬看看为什么错呢,应该怎么改?

[复制链接]
发表于 2021-2-26 15:10:18 | 显示全部楼层 |阅读模式

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

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

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

安徽.rar

266.69 KB, 下载次数: 2

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-2-26 16:41:10 | 显示全部楼层
https://blog.csdn.net/alicelmx/article/details/83902760
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-16 11:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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