类型错误:'_csv.writer'对象在 Python 数据挖掘中不可调用



尝试通过情感分析从Twitter运行简单的文本挖掘,无法弄清此错误:typeError:'_csv.writer'对象不可callable

它扎根于我的代码的这一部分:

  with io.open('trumpTweets.csv', 'w', encoding='utf8', newline='') as csvfile:
        csv_writer = csv.writer(csvfile) 
        csv_writer("Tweet", " Sentiment")
        with io.open("trumpTweets.txt", 'r', encoding='utf8') as f:
                for tweet in f.readlines():
                        tweet = tweet.strip()

总的来说,我的代码看起来像这样。

import tweepy, codecs
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
from aylienapiclient import textapi
import csv, io
import matplotlib.pyplot as plt
import pandas as pd
from collections import Counter
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
user = api.get_user('ballondebruyne')
kyle = api.get_user('KKopchak')
results = api.user_timeline(user_id='25073877', count=20)
file = codecs.open("trumpTweets.txt", "w", "utf-8")
for tweet in results:
        file.write("trumpTweets.txt")
        file.write("n")
file.close()
client = textapi.Client("276a6096", "66cf9c66a25a398c6c6f8c9b33abc771")
with io.open('trumpTweets.csv', 'w', encoding='utf8', newline='') as csvfile:
        csv_writer = csv.writer(csvfile) 
        csv_writer("Tweet", " Sentiment")
        with io.open("trumpTweets.txt", 'r', encoding='utf8') as f:
                for tweet in f.readlines():
                        tweet = tweet.strip()
                        if len(tweet) == 0:
                                print('skipped')
                                continue
                print(tweet)
                        sentiment = client.Sentiment({'text': tweet})
                        csv_writer.writerow([sentiment['text'], sentiment['polarity']])
with open('trumpTweets.csv', 'r', encoding = 'utf8') as csvfile:
        df = pd.read_csv(csvfile)
        sent = df["Sentiment"]
        counter = Counter(sent)
        positive = counter['positive']
        negative = counter['negative']
        neutral = counter['neutral']
labels = 'Positive', 'Negative', 'Neutral'
sizes = [positive, negative, neutral]
colors = ['green', 'red', 'grey']
yourtext = "President Trump"
plt.pie(sizes, labels = labels, colors = colors, shadow = True, startangle = 90)
plt.title("Sentiment of 200 Tweets about "+yourtext)
plt.show()

正在尝试对结果进行一些数据可视化,但首先希望能够在我运行时看到某些事情。毕竟,我确实拉了大部分这本FRO MA教程。

代码应正确看起来像:

with io.open('trumpTweets.csv', 'w', encoding='utf8', newline='') as csvfile:
    csv_writer = csv.writer(csvfile) 
    csv_writer.writerow(["Tweet", " Sentiment"])
    with io.open("trumpTweets.txt", 'r', encoding='utf8') as f:
        for tweet in f.readlines():
            tweet = tweet.strip()

请注意,在此代码中,第三行被更改为csv_writer.writerow("Tweet", "Sentiment")。在原始代码中,您尝试将CSV_WRITER对象用作可可对象,而您需要在对象上调用正确的方法。

您可以在https://docs.python.org/3/library/csv.html#writer-objects。

上找到CSV.Writer的文档和示例

最新更新