Tweets streamed using tweepy, reading json file in python

≡放荡痞女 提交于 2019-12-25 03:22:49

问题


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

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?


回答1:


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.

This caused a json.decoder.JSONDecodeError.

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

using odd rows:

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))

exception-handling:

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.



来源:https://stackoverflow.com/questions/53788395/tweets-streamed-using-tweepy-reading-json-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!