Python将csv数据导出到文件中

编程入门 行业动态 更新时间:2024-10-08 18:31:46
本文介绍了Python将csv数据导出到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码,但是我不能修剪和存储在数据文件中的数据:

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 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?

解决方案

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数据导出到文件中

本文发布于:2023-06-02 01:29:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/430976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   导出到   数据   Python   csv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!