我有一段时间我一直在使用的代码。我想知道是否有一种方法可以每行读取CSV文件(Twitter feed)并导出CSV中的输出。
我理想地希望每行提取名词术语,即在我的情况下是Twitter feed。
这是代码。对不起,我是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()
(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进行测试你要。如果您需要更多帮助,或者我已经误解了,请告诉我。