Python 3.7 属性错误 : 'list' object has no attribute 'split'

标签 python python-3.x attributeerror vader

我正在尝试从每行都有一个句子的 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'

your_list 上的 .split("") 函数没有帮助

最佳答案

Vader 的“极性_分数(句子)”采用字符串参数,而不是列表。

您的代码应该是:

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']

输出: Final Output

如果您希望格式化输出字符串,请对字符串格式化进行一些研究。如果您找不到答案,或者在 SO 上发布另一个问题。

关于Python 3.7 属性错误 : 'list' object has no attribute 'split' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57577132/

相关文章:

python - 如何将 jupyter notebook 目录中的模块导入到较低目录的 notebook 中?

python - 在PC上没有安装python的情况下运行python脚本

python - 当 Python 从命令行运行时导入 urllib.parse 失败

python - Matplotlib,绘制 pandas 系列 : AttributeError: 'tuple' object has no attribute 'xaxis'

python - 如何在 python 和 linux 中使用 ssh 连接到 windows 并运行命令?

python - 我如何在 Marshmallow 中将 data_key 指定为可能的键名列表?

python - 使用 scikit-learn 计算精度时出现 ValueError

python - Flask 的内置服务器总是 404 并设置了 SERVER_NAME

python - 无法从类传递调整大小的 QLabel 几何

python - 初学者 Python : AttributeError: 'list' object has no attribute