我有以下代码,但是我不能修剪和存储在数据文件中的数据:
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()它所做的是检查单词double或triple等,在列表中添加一个词。 输出如下所示:
['this','amazing','car' ,'forward','looking','love','not','the','view']$ b b
现在你可以看到我试图保存在一个文本文件中的数据,但我得到一个
TypeError:expected a字符缓冲区对象我想要一个文本文件中的数据,格式如下:
1:this 2:amazing 3:car 4:concert 5 :forward .../ p>
有人知道如何以这种方式保存我的数据吗?
解决方案p>您正在尝试将一个列表对象写入文件,但它期望一个字符串。你可以使用`enumerate here:
word_features = get_word_features(get_words_in_tweets(tweets)) with open('wordFeatures。 csv','w')作为输出: for ind,item in enumerate(word_features,1): output.write({}:{} \\\.format ))或使用 csv / p>
import csv word_features = get_word_features(get_words_in_tweets(tweets)) with open('wordFeatures.csv' ,'w')作为输出: writer = csv.writer(output,delimiter =':') writer.writerows(enumerate(word_features,1))输出:
1:this 2:amazing 3:car 4:concert 5:forward 6:looking 7:love 8:not 9:the 10:view
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 objectI 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?
解决方案You're trying to write a list object to a file, but it expects a string. You can use `enumerate 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 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
更多推荐
Python将csv数据导出到文件中
发布评论