如何抑制 Python 库 TextBlob sentiment.polarity 的某些输出

how can I suppress some output from the Python Library TextBlob sentiment.polarity

我正在使用 Python 为从 TextBlob 返回的结果分配标签。 我的基本代码如下所示:

from textblob import TextBlob

def sentLabel(blob):
    label = blob.sentiment.polarity 

    if(label == 0.0):
        print('Neutral')
    elif(label > 0.0):
        print('Positive')
    else:
        print('Negative')

    Feedback1 = "The food in the canteen was awesome"
    Feedback2 = "The food in the canteen was awful"
    Feedback3 = "The canteen has food"


    b1 = TextBlob(Feedback1)
    b2 = TextBlob(Feedback2)
    b3 = TextBlob(Feedback3)

    print(b1.sentiment_assessments)
    print(sentLabel(b1))
    print(b2.sentiment_assessments)
    print(sentLabel(b2))
    print(b3.sentiment_assessments)
    print(sentLabel(b3))

这会正确打印出情绪,但也会打印出 "None",如下所示:

Sentiment(polarity=1.0, subjectivity=1.0, assessments=[(['awesome'], 1.0, 1.0, None)])

Positive

None

...

有什么方法可以抑制 "None" 被打印出来吗?

感谢您的帮助或指点。

您的函数 sentLabel return None。因此,当您使用 print(sentLabel(b1)) 时,它会打印 None.

这应该适合你。

from textblob import TextBlob

def sentLabel(blob):
    label = blob.sentiment.polarity 

    if(label == 0.0):
        print('Neutral')
    elif(label > 0.0):
        print('Positive')
    else:
        print('Negative')

    Feedback1 = "The food in the canteen was awesome"
    Feedback2 = "The food in the canteen was awful"
    Feedback3 = "The canteen has food"


    b1 = TextBlob(Feedback1)
    b2 = TextBlob(Feedback2)
    b3 = TextBlob(Feedback3)

    print(b1.sentiment_assessments)
    sentLabel(b1)
    print(b2.sentiment_assessments)
    sentLabel(b2)
    print(b3.sentiment_assessments)
    sentLabel(b3)