0

I'm using Twitter's streaming api to extract tweets using Tweepy and write them to a json file. So far, i've extracted the tweets successfully and wrote them to a json file using file write but when i try to use json package i get error as mentioned in the code below.

 def on_data(self, data):
    #to convert data to dict format since twitter data is in string format
    json_data = json.loads(data)
    try:
        with open('twitter_data.json','ab') as f:
                if 'limit' in json_data.keys():
                    return True
                else:
                    #This method works
                    #f.write(json.dumps(json_data)+ "\n")
                    #this one does not as it concatenates dict i.e different dict are not separated by a comma
                    json.dump(json_data,f)
                    return True
    except BaseException as e:
        print e
        logging.debug('Error %s',e)
        return True

1 Answer 1

1

You get the right data but don't have the line separator... so add it yourself

import json
with open('deleteme', 'a') as fp:
    json.dump('data', fp)
    fp.write('\n')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.