pos_words = [] neg_words = [] # Read from the uploaded file with open('sentimentanalysis.txt') as file: for line in file: line_attrib = line.split() word = line_attrib[2].split('=')[1] # 2nd column in the file polarity = line_attrib[-1].split('=')[1] # last column in the file if polarity == 'positive': pos_words.append(word) elif polarity == 'negative': neg_words.append(word) # Print the counts after processing all lines print('Total positive words found: ', len(pos_words)) print('Total negative words found: ', len(neg_words)) # Write results to file for future use with open('pos_words.txt', mode='wt', encoding='utf-8') as myfile: myfile.write('\n'.join(pos_words)) with open('neg_words.txt', mode='wt', encoding='utf-8') as myfile: myfile.write('\n'.join(neg_words))