The from snownlp import snownlp
The import codecs
The import OS
Source=open (' C: \ \ Users \ \ Administrator USER - 20180430 hs python analysis \ \ \ \ li river Lijiangcomment_ general processing during 2. TXT ', encoding="utf-8"), read ()
The line=source. Readlines ()
Sentimentslist=[]
For I in line:
S=SnowNLP (i.d ecode (" utf-8 "))
Print (s.s entiments)
Sentimentslist. Append (s.s entiments)
The import matplotlib. Pyplot as PLT
The import numpy as np
PLT. Hist (sentimentslist, bins.=np arange (0, 1, 0.01), facecolor='g')
PLT. Xlabel (' Sentiments aim-listed Probability ')
PLT. Ylabel (' Quantity ')
PLT. Title (' Analysis of Sentiments')
PLT. The show ()
The results are as follows:
AttributeError Traceback (the most recent call last)
4
5 source=open (' C: \ \ Users \ \ Administrator USER - 20180430 hs python analysis \ \ \ \ li river Lijiangcomment_ general processing during 2. TXT ', encoding="utf-8"), read ()
- & gt; 6 line=source. Readlines ()
7 sentimentslist=[]
8 for I in line:
AttributeError: 'STR' object has no attribute 'readlines'
CodePudding user response:
Source=open (), read ()Read has read the content, and converted to a string
Why use readlines ()
To source=open ()
CodePudding user response:
Thank you so much! Yesterday has been resolved!