Export Python export csv data to file

I have the following code that works well, but I can’t trim and store data in a data file:

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 he does, he checks to see if the words are double or triple, etc. and adds only one word to the list. The result is as follows:

 ['this', 'amazing', 'car', 'concert', 'forward', 'looking', 'love', 'not', 'the', 'view'] 

Now, as you can see, I tried to save this data in a text file, but I get

 TypeError: expected a character buffer object 

I need data from an array in a text file in the following format:

 1:this 2:amazing 3:car 4:concert 5:forward ... 

therefore, one line for each word with an increasing integer.

Does anyone know how to save my data this way?

+4
source share
3 answers

You are trying to write a list object to a file, but it expects a string. You can use the listing here:

 word_features = get_word_features(get_words_in_tweets(tweets)) with open('wordFeatures.csv', 'w') as output: for ind, item in enumerate(word_features, 1): output.write("{}:{}\n".format(ind, item)) 

or using the csv module:

 import csv word_features = get_word_features(get_words_in_tweets(tweets)) with open('wordFeatures.csv', 'w') as output: writer = csv.writer(output, delimiter=':') writer.writerows(enumerate(word_features, 1)) 

Output:

 1:this 2:amazing 3:car 4:concert 5:forward 6:looking 7:love 8:not 9:the 10:view 
+1
source

The cause of the error is that output.write takes 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 car and concert get together on the same line. I guess this is a typo and you really need them on different lines. Then you can do this to get this output:

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

In Python, I save the data in a csv file, but rather rudely:

First, I save my data in a text file. In each row, I separate each “column element” with a comma.

Then, when I finished with this line [currently only a line in a text file], I write in a new line and start writing in the next line of data. Repeat as desired.

Then, when it's over, I will rename the text file to csv file.

For you, by adding an increasing integer, you can create an increment counter. If you were to do what I had, you could increase your counter, write the value to a text file, write to a comma, write to your data, and then write to a new line and then repeat. Just remember to rename the file to csv when everything is ready.

As I said, hacking is a way to do this, but anyway. I am open to learning the best methods.

0
source

Source: https://habr.com/ru/post/1502271/


All Articles