Python 分配前引用的局部变量TwitterAPI问题

Python 分配前引用的局部变量TwitterAPI问题,python,twitterapi-python,Python,Twitterapi Python,我正在尝试使用从推特API收集的推特上的熊猫创建一个数据框。当我尝试调用我的代码时,会出现一条错误消息(下面的屏幕截图)。这意味着我的数据框df在作业之前就被引用了,而乍一看显然不是 这是我的错误消息“UnboundLocalError:赋值前引用的局部变量'df” 主要方法 twitter_client = TwitterClient() api = twitter_client.get_twitter_client_api() tweet_analyzer = TweetAnalyzer()

我正在尝试使用从推特API收集的推特上的熊猫创建一个数据框。当我尝试调用我的代码时,会出现一条错误消息(下面的屏幕截图)。这意味着我的数据框df在作业之前就被引用了,而乍一看显然不是

这是我的错误消息“UnboundLocalError:赋值前引用的局部变量'df”

主要方法

twitter_client = TwitterClient()
api = twitter_client.get_twitter_client_api()
tweet_analyzer = TweetAnalyzer()

#go together
tweets = api.user_timeline(screen_name ="nytimes", count=200) #prints tweets from the reald donald trump
df = tweet_analyzer.tweets_to_data_frame(tweets) #get text of tweet
TWITTER\u分析器类

类tweet分析器:

def tweets_to_data_frame(self, tweets):
    count =0
    for tweet in tweets:
        #print(type(tweet))
        json_str = json.dumps(tweet._json)
        parsed = json.loads(json_str)
        #print(json.dumps(parsed, indent=4, sort_keys=True))
        #print (parsed['text'])
        y=re.findall('.+Corona.+',parsed['text'])
        #count=count+1;
        #print("Count", count)
        #print(str(tweet.text))

        if len(y)>0 and tweet.favorite_count > 6000:
            print(tweet.text)
            df = pd.DataFrame(data = [tweet.text], columns = ['tweets'])
            df['id'] = np.array([tweet.id])
            df['len'] = np.array([len(tweet.text)])
            df['date'] = np.array([tweet.created_at])
            df['source'] = np.array([tweet.source])
            df['likes'] = np.array([tweet.favorite_count])
            df['retweet'] = np.array([tweet.retweet_count])
    return df

我认为你的问题在这篇文章中得到了回答:我认为你的问题在这篇文章中得到了回答: