NLTK POS标签 - 无法导出CSV

时间:2016-06-22 14:38:27

标签: python twitter nltk

我有这个代码,我已经使用了一段时间。我想知道是否有办法在每行读取csv文件(twitter feed)并在csv中导出输出。

理想情况下,我希望每行提取名词条款,即在我的情况下提取推文。

这是代码。对不起,我不熟悉Python。

import nltk
essays = u"""text here"""
tokens = nltk.word_tokenize(essays)
tagged = nltk.pos_tag(tokens)
nouns = [word for word,pos in tagged \
	if (pos == 'NN' or pos == 'NNP' or pos == 'NNS' or pos == 'NNPS')]
downcased = [x.lower() for x in nouns]
joined = " ".join(downcased).encode('utf-8')
into_string = str(nouns)

output = open("output.txt", "w")
output.write(joined)
output.close()

1 个答案:

答案 0 :(得分:0)

(csv docs)https://docs.python.org/2/library/csv.html

import csv
all_nouns = []
with open('twitter_feed.csv', 'rb') as csvfile:
     tweetreader = csv.reader(csvfile, delimiter=',', quotechar='"')
     for tweet in tweetreader:
        tokens = nltk.word_tokenize(essays)
        tagged = nltk.pos_tag(tokens)
        nouns = [word for word,pos in tagged \
            if (pos == 'NN' or pos == 'NNP' or pos == 'NNS' or pos == 'NNPS')]
        downcased = [x.lower() for x in nouns]
        joined = ",".join(downcased).encode('utf-8')
        all_nouns.append(joined)
csv_file = csv.writer("nouns.csv")
csv_file.writerows(all_nouns)

我担心目前我的机器上没有Python来测试这个,但我基本上使用了Python文档和你的代码将这个脚本压缩在一起,这应该让你进入正确的方向来实现你要。如果您需要更多帮助或者我误解了请告诉我。