Python export csv data into file
Question: 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?


Answer: The reason for the error is that `output.write` accepts a string, not a
`list`. `word_features` is a `list`.

To write a list to a file, you will need to iterate over it:

    
    
    for feature in word_features: 
        output.write("{0}\n".format(feature))
    

I don't understand the format you need because of the `car` and `concert`
coming together on the same line. I am assuming that is a typo and you
actually need them on separate lines. Then you can do this to obtain that
output:

    
    
    for nfeature in enumerate(word_features):
        output.write("{0}:{1}\n".format(nfeature[0] + 1, nfeature[1]))
    

