Python Forum
Sentiment Analysis with NLTK Vader - Writing data in one row - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Sentiment Analysis with NLTK Vader - Writing data in one row (/thread-3321.html)



Sentiment Analysis with NLTK Vader - Writing data in one row - ulrich48155 - May-14-2017

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!


RE: Sentiment Analysis with NLTK Vader - Writing data in one row - Ofnuts - May-15-2017

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