Skip to content Skip to sidebar Skip to footer

Tweets Streamed Using Tweepy, Reading Json File In Python

I streamed tweets using the following code class CustomStreamListener(tweepy.StreamListener): def on_data(self, data): try: with open('brasil.json', 'a') as

Solution 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.


Post a Comment for "Tweets Streamed Using Tweepy, Reading Json File In Python"