如何从文本文件读取Twitter数据并在python程序中获取推文的文本

时间:2019-09-20 12:10:51

标签: python twitter

我正在尝试从文本文件tweet_file中读取Twitter数据,然后获取tweet文本并将其附加到tweet_data,然后打印该tweet文本。但是我在for t in tweet_data :行说tweet_data not defined时收到名称错误。这是我正在尝试的代码:

with open(tweet_file) as f :
    for line in f:
        response = json.loads(line)
        print response["text"]
        if "text" in response.keys():
            tweet_data.append(response["text"])
            print response["text"]
    #print response.keys()

#print len(tweet_data)

# For each tweet:
for t in tweet_data:

    total = 0

    # Convert from <type 'unicode'> to <type 'str'>
    encoded_t = t.encode('utf-8')

    words = encoded_t.split()

    # print (str(words)) 

0 个答案:

没有答案