Python Forum

Full Version: Sentiment Analysis with NLTK Vader - Writing data in one row
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello python experts,

for an university project I want to do an sentiment analysis of twitter tweets with the NLTK package. Therefore, my code looks this:

sid = SentimentIntensityAnalyzer()
for sentence in tweets.Mess:
        print(sentence)
        ss = sid.polarity_scores(sentence)
        for k in sorted(ss):
            print('{0}: {1}, '.format(k, ss[k]), end='')
            print()
            with open('OutputSentiment.csv','a') as outfile:
                outfile.write(str(sentence) + ',' + str(k) + ',' + str(ss[k]) + '\n')
The results in the output file look this:

[Image: sUzLk]

Unfortunatly, I get four rows for every tweet instead of only one. What do I have to do to fix this and is it possible to get the results of the sentiment analysis in different columns? The structure of the file should look like this:

Tweet,Compound,Negativ,Neutral,Possitive 

Thanks in advance!
Have you checked the real contents of your data? If there are terminating linefeeds, you can remove them with trim().