snownlp的情感分析不知道为什么报错AttributeError: 'str' object has no attrib

[复制链接]
查看11 | 回复2 | 2021-1-27 06:48:27 | 显示全部楼层 |阅读模式
可以5个积分作为回报。
fromsnownlpimportSnowNLP
importcodecs
importos
source=open('C:\\Users\\Administrator.USER-20180430HS\\漓江python分析\\Lijiangcomment_一般性行处理2.txt',encoding='UTF-8').read()
line=source.readlines()
sentimentslist=[]
foriinline:
s=SnowNLP(i.decode("utf-8"))
print(s.sentiments)
sentimentslist.append(s.sentiments)
importmatplotlib.pyplotasplt
importnumpyasnp
plt.hist(sentimentslist,bins=np.arange(0,1,0.01),facecolor='g')
plt.xlabel('SentimentsProbability')
plt.ylabel('Quantity')
plt.title('AnalysisofSentiments')
plt.show()
运行结果如下:
AttributeErrorTraceback(mostrecentcalllast)
in
4
5source=open('C:\\Users\\Administrator.USER-20180430HS\\漓江python分析\\Lijiangcomment_一般性行处理2.txt',encoding='UTF-8').read()
---->6line=source.readlines()
7sentimentslist=[]
8foriinline:
AttributeError:'str'objecthasnoattribute'readlines'
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:48:27 | 显示全部楼层
source=open().read()
读取已经将内容读取出来,并且转变成字符串了
为什么还要用readlines()
改成source=open()就行了
回复

使用道具 举报

千问 | 2021-1-27 06:48:27 | 显示全部楼层
感谢您!昨天已经解决啦!
回复

使用道具 举报

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

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行