0

I'm using twython library in python to dump public tweets of my own. Data are downloaded in json format refer : https://api.twitter.com/1.1/statuses/home_timeline.json

How to print all data line by line, like

    print "Tweet : %s" %tweet['text']#status
    print "Create Time : %s" %tweet['created_at']#time of tweet
    print "Geo location : %s" %tweet['geo']#geo location if avail
    print "Favorite Count : %s" %tweet['favorite_count']
    print "Source : %s" %tweet["source"]
    print "Retweeted : %s" %tweet["retweeted"]
    print "contributors :%s" %tweet["contributors"]
    print "truncated : %s" %tweet["truncated"]
    print "is_quote_status : %s" %tweet["is_quote_status"]
    print "in_reply_to_status_id : %s" %tweet["in_reply_to_status_id"]
    print "Unique ID : %s" %tweet["id"]
    print "coordinates : %s" %tweet["coordinates"]
    print "in_reply_to_screen_name : %s" %tweet["in_reply_to_screen_name"]
    print "retweet_count : %s" %tweet["retweet_count"]
    print "in_reply_to_user_id : %s" %tweet["in_reply_to_user_id"]
    print "favorited :%s" %tweet["favorited"]
jOSe
  • 687
  • 11
  • 22
  • If you have the json, you can convert it to python `dict` using `json.loads`. – Barun Sharma Jul 22 '15 at 09:37
  • Don't complicate the things, you can use `Tweepy` module for accessing the tweets. – ZdaR Jul 22 '15 at 09:38
  • Any sample codes to get started. The format of json output will be available in the link https://api.twitter.com/1.1/statuses/home_timeline.json – jOSe Jul 22 '15 at 09:39
  • @zdaR tweepy, is it easy as twython? Any pro and con would you like to point out? No much methods available for user timeline http://tweepy.readthedocs.org/en/v3.2.0/api.html#API.user_timeline – jOSe Jul 22 '15 at 09:40
  • Maybe you could consider using `nltk`. There are new some functions to read twitter jsons, although not yet released. Please take a look to this [notebook](https://github.com/nltk/nltk/blob/develop/nltk/test/twitter.ipynb), paragraph *Extracting Parts of a Tweet*. If you wanted to use it, you have to download the project source and add it to your PYTHONPATH. No idea when the actual release will be. (By the way, this is using `twython` for accessing twitter. Hope it helps. – lrnzcig Jul 23 '15 at 09:23

1 Answers1

1

Considering that you have got the tweet in json format using twython and it looks something like:- "{'text' : 'abc', 'created_at': '<created_date>'}"

You can use python json like:-

>>import json
>>tweet_json = <your_json>
>>python_datastruct = json.loads(tweet_json)

Above sample will return you a python data structure whic you can use to print the required info.

EDIT: For a nested object , try something like:-

global_dict = {'a':{'a1':{'a11':1, 'a12':2}, 'a2':3}, 'b':4}
def print_recur(py_item):
    for key, value in py_item.items():
        print key
        if type(value) == dict:
            print_recur(value)
        else:
            print value

print_recur(global_dict)

This would iterate over your nested dictionary to print all keys and values.

Barun Sharma
  • 1,452
  • 2
  • 15
  • 20
  • What happens if the value are nested into another value? How to `print` the nested value automatically? May sample data {u'contributors': None, u'truncated': False, u'text': u'Dumping data for art. #dataviz', u'is_quote_status': False, u'in_reply_to_status_id': None, u'id': 623497430017839104, u'favorite_count': 0, u'source': u'Twitter Web Client', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [], u'hashtags': [{u'indices': [22, 30], u'text': u'dataviz'}], u'urls': []},continue... – jOSe Jul 22 '15 at 09:46
  • You can try printing `python_datastruct` . This would dump all the data. So you will get the structure of received data. Mostly the structure will be same for all received tweets. And hence you can have a generic function to print your customized data. – Barun Sharma Jul 22 '15 at 09:49
  • Generic functions ? Like. Is it similar to this http://stackoverflow.com/questions/23306653/python-accessing-nested-json-data – jOSe Jul 22 '15 at 09:50
  • Please check the edit....and yes I ment something like what you have pointed. But make it more generic. :) – Barun Sharma Jul 22 '15 at 10:07
  • Thanks that was great help, but still one more nested value are stuck. – jOSe Jul 22 '15 at 10:38
  • can you post your `json string` or atleast the structure of it. I will try to figure out the issue. – Barun Sharma Jul 22 '15 at 11:00