2016-06-22 7 views
0

Ich habe diesen Code, den ich seit einiger Zeit verwende. Ich frage mich, ob es eine Möglichkeit gibt, CSV-Datei pro Zeile (Twitter-Feeds) zu lesen und die Ausgabe in CSV zu exportieren.NLTK POS Tags - CSV kann nicht exportiert werden

Ich bin im Idealfall auf der Suche nach Substantiv Begriffe pro Zeile, d. H. In meinem Fall ein Twitter-Feed.

Hier ist der Code. Es tut mir leid, aber ich bin neu in Python.

import nltk 
 
essays = u"""text here""" 
 
tokens = nltk.word_tokenize(essays) 
 
tagged = nltk.pos_tag(tokens) 
 
nouns = [word for word,pos in tagged \ 
 
\t 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()

Antwort

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) 

Ich fürchte, ich habe nicht Python auf meinem Rechner im Moment dies zu testen, aber ich habe grundsätzlich verwendet, die Python-Dokumente und Ihr Code, um dieses Skript zusammen zu zippen, was Sie in die richtige Richtung bringen sollte, um das zu erreichen, was Sie wollen. Wenn Sie weitere Hilfe benötigen oder mich missverstanden haben, lassen Sie es mich bitte wissen.