I have following code which works well but I am not able to trim and store a data in a datafile:
import nltk
tweets = [
(['love', 'this', 'car']),
(['this', 'view', 'amazing']),
(['not', 'looking', 'forward', 'the', 'concert'])
]
def get_words_in_tweets(tweets):
all_words = []
for (words) in tweets:
all_words.extend(words)
return all_words
def get_word_features(wordlist):
wordlist = nltk.FreqDist(wordlist)
word_features = wordlist.keys()
return word_features
output = open('wordFeatures.csv','w')
word_features = get_word_features(get_words_in_tweets(tweets))
print (word_features)
output.write(word_features)
#print (wordlist)
output.close()
What it does is, it checks if words a double or triple etc and only adds one word in the list. The output looks like this:
['this', 'amazing', 'car', 'concert', 'forward', 'looking', 'love', 'not', 'the', 'view']
Now as you can see I tried to save this data in a textfile but I get an
TypeError: expected a character buffer object
I want the data from the array in a textfile in the following format:
1:this
2:amazing
3:car
4:concert
5:forward
...
so one row for every word with an increasing integer.
Has someone an idea how to save my data in this way?
'car', 'concert'will come on the same line?