无法摆脱TypeError:' str'对象不可调用

时间:2015-10-04 15:57:05

标签: python string object callable

我试图在ipython笔记本中制作/训练一个Twitter情绪分析器,并且我遇到了一段代码的严重问题:

import csv

#Read the tweets one by one and process it
inpTweets = csv.reader(open('SampleTweets.csv', 'rb'), delimiter=',',    quotechar='|')
tweets = []
 for row in inpTweets:
    sentiment = row[0]
    tweet = row[1]
    processedTweet = processTweet(tweet)
    featureVector = getFeatureVector(processedTweet, stopwords)
    tweets.append((featureVector, sentiment));
#end loop

我收到了这个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-10-bbcb1b9f05f4> in <module>()
      7     sentiment = row[0]
      8     tweet = row[1]
----> 9     processedTweet = processTweet(tweet)
     10     featureVector = getFeatureVector(processedTweet, stopwords)
     11     tweets.append((featureVector, sentiment));

TypeError: 'str' object is not callable

帮助会非常棒,谢谢!

1 个答案:

答案 0 :(得分:0)

此处您的processedTweet应该是str,因此您无法调用它。

示例 -

>>> a = 'apple'
>>> a(0)

Traceback (most recent call last):
  File "<pyshell#212>", line 1, in <module>
    a(0)
TypeError: 'str' object is not callable

但是当我使用索引时,它很好。可调用意味着您将其用作sum等函数。

>>> a[0]
'a'