【问题标题】:Can't get rid of TypeError: 'str' object is not callable无法摆脱 TypeError: 'str' object is not callable
【发布时间】:2016-01-01 07:56:13
【问题描述】:

我正在尝试在 ipython notebook 中制作/训练一个 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

帮助会非常好,谢谢!

【问题讨论】:

  • processTweet 在您显示的代码中未定义。

标签: python string object callable


【解决方案1】:

您的已处理推文应该是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'

【讨论】:

  • 谢谢!对不起,我是个白痴……但我该怎么办?
  • 我不知道你的api,你需要检查你的processedTweet,它应该有错误或者你需要以不同的方式使用它。
  • 它看起来需要处理你的推文,所以你应该在某处拥有它作为函数。但同样,不知道这是如何实现的。希望对您有所帮助。
  • @RachelDbbs,如果有帮助,您可以投票并接受答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2014-10-19
  • 2023-03-18
  • 2019-11-20
  • 2021-04-21
  • 2011-04-26
相关资源
最近更新 更多