Python 3.7 AttributeError: 'list' object has no attribute 'split'
Python 3.7 AttributeError: 'list' object has no attribute 'split'
我正在尝试从每行都有一个句子的 csv 文件中进行情绪分析。
代表:
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)
但是,我收到以下错误:
analyser = SentimentIntensityAnalyzer()
def print_sentiment_scores(sentence):
snt = analyser.polarity_scores(sentence)
print("{:-<40} {}".format(sentence, str(snt)))
print_sentiment_scores(your_list)
回溯(最近调用最后):
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'
.split(" ") your_list 上的函数没有帮助
SentimentIntensityAnalyzer.polarity_scores(text)
作为参数 text/str。你传递列表列表。可能您想将文件的全部内容作为单个文本传递或单独传递每个句子,而不是列表列表。
问题出在函数'print_sentiment_scores'的调用上。传递的参数是一个列表。根据您的要求,我认为您可以传递列表的元素来进行拆分。 Split() 根据文档 here
适用于字符串类型
Vader 的 'polarity_scores(sentence)' 采用字符串参数,而不是列表。
您的代码应该是:
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']
输出:
如果要格式化输出字符串,请对字符串格式进行一些研究。或者 post 关于 SO 的另一个问题,如果你找不到答案。
我正在尝试从每行都有一个句子的 csv 文件中进行情绪分析。
代表:
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)
但是,我收到以下错误:
analyser = SentimentIntensityAnalyzer()
def print_sentiment_scores(sentence):
snt = analyser.polarity_scores(sentence)
print("{:-<40} {}".format(sentence, str(snt)))
print_sentiment_scores(your_list)
回溯(最近调用最后):
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'
.split(" ") your_list 上的函数没有帮助
SentimentIntensityAnalyzer.polarity_scores(text)
作为参数 text/str。你传递列表列表。可能您想将文件的全部内容作为单个文本传递或单独传递每个句子,而不是列表列表。
问题出在函数'print_sentiment_scores'的调用上。传递的参数是一个列表。根据您的要求,我认为您可以传递列表的元素来进行拆分。 Split() 根据文档 here
适用于字符串类型Vader 的 'polarity_scores(sentence)' 采用字符串参数,而不是列表。
您的代码应该是:
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']
输出:
如果要格式化输出字符串,请对字符串格式进行一些研究。或者 post 关于 SO 的另一个问题,如果你找不到答案。