Python 3.7 AttributeError:“列表"对象没有属性“分割" [英] Python 3.7 AttributeError: 'list' object has no attribute 'split'
问题描述
我正在尝试从csv文件中进行情感分析,其中每行都有一个句子.
I am trying to do a sentiment analysis from a csv file where each row has a sentence.
代表:
print(your_list)
[['Patience and Kindness and I know they truly love and care for animals, my dog also enjoys the events like seeing Santa and the Easter Bunny'], ['They are so sweet to my pets and try to fit them into the schedule when needed'], ['they call and check on our pet a day or 2 after visit make sure we fully understand treatment before we leave'], ['every member of the staff understands how our pets are our family; we never feel rushed and always have or questions answered, and are given reassurance if and when needed; they are compassionate and kind, respectful and very caring'], ['They made it a very peaceful experience when we had to put our pug to sleep '], ['They interact with my dogs and you can see the care they have for them.'], ['they make every effort to accomodate us']
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import csv
with open('Before.csv', "r", errors='ignore') as f:
reader = csv.reader(f)
your_list = list(reader)
print(your_list)
analyser = SentimentIntensityAnalyzer()
def print_sentiment_scores(sentence):
snt = analyser.polarity_scores(sentence)
print("{:-<40} {}".format(sentence, str(snt)))
print_sentiment_scores(your_list)
但是,我收到以下错误:
However, I receive the below error:
analyser = SentimentIntensityAnalyzer()
def print_sentiment_scores(sentence):
snt = analyser.polarity_scores(sentence)
print("{:-<40} {}".format(sentence, str(snt)))
print_sentiment_scores(your_list)
回溯(最近通话最近一次):
Traceback (most recent call last):
File "<ipython-input-24-a7a32425d261>", line 8, in <module>
print_sentiment_scores(your_list)
File "<ipython-input-24-a7a32425d261>", line 4, in print_sentiment_scores
snt = analyser.polarity_scores(sentence)
File "C:\Users\abc\AppData\Local\Continuum\anaconda3\lib\site-packages\vaderSentiment\vaderSentiment.py", line 248, in polarity_scores
text_token_list = text.split()
AttributeError: 'list' object has no attribute 'split'
your_list上的
.split(")函数没有帮助
.split(" ") function on your_list is not helping
推荐答案
Vader的'polarity_scores(sentence)'使用字符串参数,而不是列表.
Vader's 'polarity_scores(sentence)' takes a string parameter, not a list.
您的代码应为:
analyser = SentimentIntensityAnalyzer()
def print_sentiment_scores(alist):
for aSentence in alist:
aSnt = analyser.polarity_scores(aSentence[0])
print(str(aSnt))
print_sentiment_scores(your_list)
所以我终于可以将其与以下代码和csv一起使用:
#!/usr/bin/python3
import csv
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
with open('Before.csv', "r", errors='ignore') as f:
reader = csv.reader(f)
your_list = list(reader)
analyser = SentimentIntensityAnalyzer()
def print_sentiment_scores(alist):
for aSentence in alist:
aSnt = analyser.polarity_scores(aSentence[0])
print(str(aSnt))
print_sentiment_scores(your_list)
.csv的相关内容:
['Patience and Kindness and I know they truly love and care for
animals, my dog also enjoys the events like seeing Santa and the Easter
Bunny'], ['They are so sweet to my pets and try to fit them into the
schedule when needed'], ['they call and check on our pet a day or 2 after
visit make sure we fully understand treatment before we leave'], ['every
member of the staff understands how our pets are our family; we never feel
rushed and always have or questions answered, and are given reassurance if
and when needed; they are compassionate and kind, respectful and very
caring']
输出:
如果要格式化输出字符串,请对字符串格式进行一些研究.或者,如果找不到答案,则在SO上发布另一个问题.
If you want the output strings to be formatted, please do some research on string-formatting. Or post another question on SO, if you cannot find an answer.
这篇关于Python 3.7 AttributeError:“列表"对象没有属性“分割"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!