Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a JSON file. I opened it in Python but could not understand what to do with it. If the same data was in a CSV file, I could have used it in Python to do any sort of analysis. Please give me the entire code for that. Also below the Twitter credentials have already been changed.

Thanks

What I have tried:

Here is the code to make the JSON file, but it will not help anyone because this is correct and I need a CSV file. So whatever has to be done is in another python file.


Python
import tweepy
import json
import time

#Twitter API credentials
consumer_key = "tUnfouXORfnaVNEAyTrLmW2ZU"
consumer_secret = "Yxmd1sLKqp2YwXzJ5IJjaVO6PtrOeq1lKyl5AS2Zu2zktjYZKQ"
access_key = "1215780002-2fC55jHbZ4X7NDHgKFJMO1g63Aw0jn1zdmhJjs8"
access_secret = "MJfwXrZ9hKvfb8EUba7eoKlu5BIPDwRDKAXHZOBPdPc2p"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
#refer http://docs.tweepy.org/en/v3.2.0/api.html#API
#tells tweepy.API to automatically wait for rate limits to replenish

#Put your search term
searchquery = "#Demonetisation"

users =tweepy.Cursor(api.search,q=searchquery).items()
count = 0
errorCount=0

file = open('search.json', 'w') 

while True:
        try:
            user = next(users)
            count += 1
        
        except tweepy.TweepError:
        
            print ("sleeping....")
            user = next(users)
        except StopIteration:
            break
        try:
            print ("Writing to JSON tweet number:"+str(count))
            json.dump(user._json,file,sort_keys = True,indent = 4)
        
        except UnicodeEncodeError:
            errorCount += 1
            print ("UnicodeEncodeError,errorCount ="+str(errorCount))

print ("completed, errorCount ="+str(errorCount)+" total tweets="+str(count))
    
    #todo: write users to file, search users for interests, locations etc.
Posted
Updated 7-Jun-18 8:46am
v3
Comments
Richard MacCutchan 7-Jun-18 4:07am    
Your question is unclear. First you say you have a JSON file, then you say you are making a JSON file. Which is it, and why do you think CSV would improve things?
Member 13225387 7-Jun-18 7:15am    
I meant that this is the code which creates a JSON file and writes data in it. But it is not in the form of rows and columns. Hence it is difficult for me to work on it. Can you help?
Richard MacCutchan 7-Jun-18 7:18am    
Sorry, that does not help. I suggest you edit your question, show the properly formatted code, and explain exactly what the problem is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900