简体   繁体   English

Python将csv数据导出到文件中

[英]Python export csv data into file

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? 有人知道如何以这种方式保存我的数据?

The reason for the error is that output.write accepts a string, not a list . 出错的原因是output.write接受一个字符串,而不是一个list word_features is a list . word_features是一个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. 我不明白你需要的格式,因为carconcert在同一条线上汇集在一起​​。 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]))

You're trying to write a list object to a file, but it expects a string. 您正在尝试将列表对象写入文件,但它需要一个字符串。 You can use `enumerate here: 你可以在这里使用`enumerate:

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 : 或使用csv模块:

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

In Python, I save data into a csv file, but in a rather hack way: 在Python中,我将数据保存到csv文件中,但是以一种相当黑客的方式:

First I save my data into a text file. 首先,我将数据保存到文本文件中。 In each row, I separate each "column element" with a comma. 在每一行中,我用逗号分隔每个“列元素”。

Then, when I'm done with that row [currently just a line in a text file], I write in a new line and begin writing in the next line of data. 然后,当我完成该行[当前只是文本文件中的一行]时,我写一个新行并开始写入下一行数据。 Repeat as desired. 根据需要重复。

Then, when I'm all done, I re-name the text file into a csv file. 然后,当我完成所有操作后,我将文本文件重命名为csv文件。

For you, adding in the increasing integer, you could make up an increment counter. 对于您,添加增加的整数,您可以组成增量计数器。 If you were to do as I have, you could increment your counter, write the value into the text file, write in the comma, write in your data, and then write in a new line, then repeat. 如果您按照我的方式进行操作,可以递增计数器,将值写入文本文件,写入逗号,写入数据,然后写入新行,然后重复。 Just remember to re-name the file into a csv file when you're all done. 只需记住在完成所有操作后将文件重命名为csv文件。

Like I said, a hack way of doing it but whichever. 就像我说的那样,一种黑客的做法,但无论如何。 I'm open to hearing better methods. 我很乐意听到更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM