使用tweepy流式传输的推文,在python中读取json文件 [英] Tweets streamed using tweepy, reading json file in python

查看:344
本文介绍了使用tweepy流式传输的推文,在python中读取json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码流化了推文

I streamed tweets using the following code

class CustomStreamListener(tweepy.StreamListener):
    def on_data(self, data):
        try:
            with open('brasil.json', 'a') as f:
                f.write(data)
                return True
        except BaseException as e:
            print("Error on_data: %s" % str(e))
        return True

现在我有一个json文件(brasil.json).我想在python上打开它进行情感分析,但是我找不到办法.我设法使用以下方法打开了第一条推文:

Now I have a json file (brasil.json). I want to open it on python to do sentiment analysis but I can't find a way. I managed to open the first tweet using this:

with open('brasil.json') as f:
    for line in f:
        tweets.append(json.loads(line))

,但它不会读取所有其他推文.有什么主意吗?

but it doesn't read all the other tweets. Any idea?

推荐答案

来自注释:检查json数据文件的内容后,所有tweets均以奇数表示(如果有行).偶数为空.

From comments: after examining the contents of the json data-file, all the tweets are in the odd number if rows. The even numbers are blank.

这引起了json.decoder.JSONDecodeError.

有两种方法可以处理此错误,要么只读取奇数行,要么使用异常处理.

There are two ways to handle this error, either read only the odd rows or use exception-handling.

使用奇数行:

with open('brasil.json') as f:
    for n, line in enumerate(f, 1):
        if n % 2 == 1: # this line is in an odd-numbered row
            tweets.append(json.loads(line))

异常处理:

with open('brasil.json', 'r') as f:
    for line in f:
        try:
            tweets.append(json.loads(line))
        except json.decoder.JSONDecodeError:
            pass # skip this line 

尝试看看哪个效果最好.

try and see which one works best.

这篇关于使用tweepy流式传输的推文,在python中读取json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆